When I create an index on SQL Server 2008 R2 do i need to run update statistics after?
No you do not need to update statistics immediately after creating index.
When you create index, statistics are created with 100% of data. Running update statistics with default setting will overwrite it with sample.
As you data change (insert/update/delete) you will need to periodically update statistics to make sure your changes are reflected in your statistics. If you auto update statistics is on it will update itself when 20%+500 row changes, using sample. If sample is not good enough for your application you can consider manual update statistics.
Related
I have an instance of Aurora MySQL v2.10.2
I am trying to alter a small table (3k rows) to add a new column.
This is a prod table and is constantly queried/updated.
The alter command is getting stuck and it also blocks all the other running queries in the background. By stuck, I mean its running for more than 1 min and all the queries including the alter statement is in Waiting for table metadata lock state.
This should not take more than a few seconds though.
I can not upgrade to version 3 or change the lab settings as described here to enable Fast/Instant DDL: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Managing.FastDDL.html#AuroraMySQL.Managing.FastDDL-v2
Is there anything I can check for to get this alter to run.
I have tried these so far and each of it gets stuck.
ALTER TABLE table ADD COLUMN `my_col` int DEFAULT 100 AFTER another_col;
ALTER TABLE table ADD COLUMN `my_col` int;
ALTER TABLE table ADD COLUMN `my_col` int NULL;
It sounds like you are running into an issue with the table metadata lock when trying to alter your table in an Aurora MySQL v2.10.2 instance. This can happen when the table is being constantly queried/updated, as you mentioned.
Here are a few things you can try to resolve this issue:
Try to reduce the workload on the table during the alter operation. You can do this by temporarily disabling updates to the table or by redirecting queries to a replica.
Increase the innodb_buffer_pool_size parameter in the MySQL configuration file. This parameter controls the amount of memory used for caching data and index pages, and increasing it can help reduce the impact of table locks.
Increase the innodb_lock_wait_timeout parameter in the MySQL configuration file. This parameter controls the time that a session waits for a lock before giving up and returning an error. By increasing this value, you can allow the alter statement more time to complete.
Try running the alter statement during a maintenance window or low-usage period.
Try breaking the ALTER command into multiple commands. For example, you can create a new table with the new column and then use a SELECT INTO statement to transfer the data, after that you can drop the original table and rename the new table.
If none of the above solutions work, you might consider using the "pt-online-schema-change" tool from Percona. This tool can perform the alter table operation
Using MYSQL, I want to record my data from the general_log table on server A to a table on server B instantly at every data and delete the data from server A at the end of the day. I tried to use Trigger for this, but the general_log does not allow me to write triggers because it sees the system file. Alternatively, when I use the Fedareted table, when I delete the data on server A, those on server B are also deleted. Thanks in advance for your help.
I would recommend the following strategy:
First, partition the data on in general_log by date. You can learn about table partitioning in the documentation.
Second, set up replication so server B is identify to server A in real time. Once again, you may need to refer to the documentation.
Third, set up a job to remove the previous partition from A shortly after midnight.
To be honest, if you don't understand table partitioning and replication, you should get a DBA involved. In fact, if you are trying to coordinate multiple database servers, you should have a DBA involved, who would understand these concepts and how best to implement them in your environment.
I recommend to develop an ETL job to move the data every day and delete it from the old server
I'm fairly new to SSIS and am doing a simple migration of data from a DB2 Server table to a SQL table. In deciding how to perform an update of the data, I elected to use a Staging Table to store all the rows that have changed in some way and need to be updated. My goal is to just do a set based update from that Staging Table to the destination to avoid having to do a row-by-row update.
The problem I am running into, and maybe it isn't a problem, maybe I'm just a newb; but, I was wondering...Does the Staging Table need to be in the same database as the table that is needing to be updated?
No, it can be in a different database if you want it to be.
Well, your question makes some sense.
In general ETL scenario - no, tables can be from different DBs.
However, in Microsoft SQL, there is a technique called partition switching for speedy ETL process. In this technique, you partition your destination table (for example, on weekly basis), and update the whole week data by switching complete partition to staging table, updating data and switching staging table back to partition being modified. In this particular case tables have to be in the same DB.
I have a table with 4mil+ records. There is a staging table that gets updated with data via an ETL process throughout the day. Once the staging table gets updated, I need to then sync that data with the production table. I'm currently using an INSERT/ON DUPLICATE KEY UPDATE query to sync them, however with the size of this table it takes ~750 seconds to run. Is there a more efficient way to update/insert the new data? I have read some about partitioning tables, but I'm not sure if that's what I need to do or not. Can anyone give me some suggestions on how to do accomplish this more efficiently?
I would use the maatkit tools (http://www.maatkit.org/), specifically http://www.maatkit.org/doc/mk-table-sync.html. It is pretty efficient at this sort of thing.
my question is about a database history or transaction log table which is currently updated by mysql procedure. The procedure is called by mysql trigger every time when we keep a history of an appropriate table in during insert, update or delete actions. As far as we have lots of tables for each of them we need to create a separate trigger e.g. for "accounts table" we need to create "accounts_insert, accounts_update and accounts_delete" triggers.
The problem is every time when we alter "accounts table" we have to modify appropriate triggers as well.
Is there any way to avoid that manual work? Would it be better to implement it in application layer/code?
There are no 'global' triggers if that's what you're thinking about.
Application side logging is one possible solution. You'll want to do this within transactions whenever possible.
Other possible approaches:
Create a script that will update your triggers for you. Can be fairly easy, if your triggers are generally similar to each other. Using information_schema database can be helpful here.
Parse general query log (careful, enabling this log can have large negative impact on server's performance)