“select count(id) from table” takes up to 30 minutes to calculate in SQL Azure

前端 未结 3 1312
野趣味
野趣味 2021-02-07 04:30

I have a database in SQL Azure which is not taking between 15 and 30 minutes to do a simple:

select count(id) from mytable

The database is abou

相关标签:
3条回答
  • 2021-02-07 04:46

    Suggestion: try select count(*) instead: it might actually improve the response time:

    • http://www.sqlskills.com/blogs/paul/which-index-will-sql-server-use-to-count-all-rows/

    Also, have you done an "explain plan"?

    • http://azure.microsoft.com/blog/2011/12/15/sql-azure-management-portal-tips-and-tricks-part-ii/

    • http://social.technet.microsoft.com/wiki/contents/articles/1657.gaining-performance-insight-into-windows-azure-sql-database.aspx

    ============ UPDATE ============

    Thank you for getting the statistics.

    You're doing a full table scan of 2M rows - not good :(

    POSSIBLE WORKAROUND: query system table row_count instead:

    http://blogs.msdn.com/b/arunrakwal/archive/2012/04/09/sql-azure-list-of-tables-with-record-count.aspx

    select t.name ,s.row_count from sys.tables t
    join sys.dm_db_partition_stats s
    ON t.object_id = s.object_id
      and t.type_desc = 'USER_TABLE'
      and t.name not like '%dss%'
      and s.index_id = 1
    
    0 讨论(0)
  • 2021-02-07 05:10

    I realize this is old, but I had the same issue. I had a table with 2.5 million rows that I imported from an on-prem database into Azure SQL and ran at S3 level. Select Count(0) from Table resulted in a 5-7 minute execution time vs milliseconds on-premise.

    In Azure, index and table scans seem to be penalized tremendously in performance, so adding a 'useless' WHERE to the query that forces it to perform an index seek on the clustered index helped.

    In my case, this performed almost identical Select count(0) from Table where id > 0 resulted in performance matching the on premise query.

    0 讨论(0)
  • 2021-02-07 05:11

    Quick refinement of @FoggyDay post. If your tables are partitioned, you'll want to sum the rowcount.

    SELECT t.name, SUM(s.row_count) row_count
    FROM sys.tables t
    JOIN sys.dm_db_partition_stats s
    ON t.object_id = s.object_id
      AND t.type_desc = 'USER_TABLE'
      AND t.name not like '%dss%'
      AND s.index_id = 1
    GROUP BY t.name
    
    0 讨论(0)
提交回复
热议问题