One 400GB table, One query - Need Tuning Ideas (SQL2005)

后端 未结 24 2132
予麋鹿
予麋鹿 2021-01-30 15:03

I have a single large table which I would like to optimize. I\'m using MS-SQL 2005 server. I\'ll try to describe how it is used and if anyone has any suggestions I would appreci

24条回答
  •  时光取名叫无心
    2021-01-30 15:41

    You can try:

    alter table MyTable
        add constraint PK_MyTable
            primary key nonclustered (k1, k2)
    create clustered index IX_MyTable
        on MyTable(k4, k1, k3, k5, k6, k7)
        --decreasing order of cardinality of the filter columns
    

    This will ensure that your duplicate inserts continue to error out.

    This may also instruct SQL Server to filter on (k1, k3, k4, k5, k6) and order on (k7 asc) in one pass, permitting SQL Server to stream the query results without the intermediate step of sorting a million results first. Once SQL Server finds the first row matching (k1, k3, k4, k5, k6), the next million rows or so rows will all match the same filter, and will already be in sorted order by (k7 asc). All filtering and ordering will be done, together, based on the clustered index.

    Provided the pages are stored consecutively, and provided SQL Server knows how to optimize, that's a few disk seeks to walk down the index to find the first matching row followed by one big sequential disk read of ten thousand or so pages. That should be faster than asking SQL Server to seek all over the place to find the rows and then asking SQL Server to sort them in tempdb!

    You will have to be vigilant and ensure that the clustered index is in good health at all times. You may also have to reduce the page fill factor if insert time slows down too much.

提交回复
热议问题