我有2個表格('鍵'由大約6個字段組成,'stats'由大約65個字段組成)。如何優化SQL查詢?
我想在兩個表中插入行而不公開短語文本。我使用這樣的東西:
UPDATE Keys SET CommandType = 'ADDED', CommandCode = @CommandCode WHERE
KeyText = @KeyText AND Tab_ID = @TabID AND CommandType = 'DELETED';
INSERT INTO Keys (IsChecked, KeyText, AddDateTime, Tab_ID, KeySource_ID, CommandCode, CommandType)
SELECT 0, @KeyText, datetime(), @TabID, @KeySourceID, @CommandCode, 'ADDED'
WHERE NOT EXISTS (SELECT 1 FROM Keys WHERE Tab_ID = @TabID AND KeyText = @KeyText);
INSERT INTO Statistics (Key_ID)
SELECT ID FROM Keys WHERE KeyText = @KeyText AND Tab_ID = @TabID AND (CommandType IS NULL OR CommandType <> 'DELETED') AND
NOT EXISTS (SELECT 1 FROM Statistics WHERE Key_ID = (SELECT ID FROM Keys WHERE KeyText = @KeyText AND Tab_ID = @TabID AND (CommandType IS NULL OR CommandType <> 'DELETED') LIMIT 1));
我該如何優化它?我爲這個查詢字段中使用的所有索引創建索引。也許你可以推薦我一些解決方案?
感謝您的幫助和抱歉,我的英語不好。
什麼是需要優化的?需要太長時間才能插入? – Randy
您是否嘗試過SQL Profiler? –
是啊!問題出在我的表格的糟糕索引上。我重構它,並用靜態內容替換一些查詢參數,它效果很好! 感謝您的幫助! – Dmitriy