Speeding Up Update SQL Query
I am using following SQL over a SQL 2000 table, which contain a开发者_C百科round 100,000 records. It is very slow.
How can I speed this up ?
UPDATE [MASTER]
SET [STATUS] = 1
WHERE [CODE] IN (" + *customerCodes* + ")";
customerCodes is passed from a UI where user can check customers from a grid.
The most obvious place to start with improving this query is to look at the indexes you have on your [MASTER] table (not a great name for a table, by the way, I'd recommend renaming it to something less generic, particulary since the SQL Server system database is called master).
You want to make sure that [CODE] has an index on it.
From there the next step is to start looking at the query execution plan.
In SQL Server Management Studio (or Query Analyzer on SQL 2000) there is the ability to view the query execution plan. I don't have a copy of Query Analyzer handy, so I can only say that in SSMS you view the execution plan by selecting Query -> Include Actual Execution Plan. The next time you run the query you will then be able to see how the query was executed. You will want to look to ensure that you are always hitting indexes in your query, and the you are not performing low performance operations like Table Scans.
One thing to be aware of with the query execution plan is that it will show a different execution plan on large sets of data than on small, so to tune a large query you need a large set of test data.
The final thing that may be worth trying is to convert your customerCodes variable (which I'm guessing is something like: '2, 6, 10, 5', that is a comma separated list of ids) into the result of a table valued function. I'm not sure that this would give the query optimizer more to work with, but it might.
Well, the simplest way is to make sure the 'where' clause uses a field that is indexed. (So this implies you change your interface so that you can get the indexed field (typically an ID) instead of whatever you're currently doing).
精彩评论