Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Mikael Eriksson has a good explanation bellow why the first query is fast:</p> <p>SQL server optimize it into: <code>if exists(select * from BookChapters)</code>. So it goes looking for the presence of one row instead of counting all the rows in the table.</p> <p>For the other two queries, SQL Server would use the following rule. To perform a query like <code>SELECT COUNT(*)</code>, SQL Server will use the narrowest <strong>non-clustered</strong> index to count the rows. If the table does not have any non-clustered index, it will have to scan the table.</p> <p>Also, if your table has a <strong>clustered</strong> index you can get your count even faster using the following query (borrowed from this site <a href="http://dbatipster.blogspot.com/2009/08/get-row-counts-fast.html" rel="noreferrer">Get Row Counts Fast!</a>)</p> <pre><code>--SQL Server 2005/2008 SELECT OBJECT_NAME(i.id) [Table_Name], i.rowcnt [Row_Count] FROM sys.sysindexes i WITH (NOLOCK) WHERE i.indid in (0,1) ORDER BY i.rowcnt desc --SQL Server 2000 SELECT OBJECT_NAME(i.id) [Table_Name], i.rows [Row_Count] FROM sysindexes i (NOLOCK) WHERE i.indid in (0,1) ORDER BY i.rows desc </code></pre> <p>It uses sysindexes system table. More info you can find here <a href="http://msdn.microsoft.com/en-us/library/aa260413%28v=sql.80%29.aspx" rel="noreferrer">SQL Server 2000</a>, <a href="http://msdn.microsoft.com/en-us/library/ms173760%28v=sql.90%29.aspx" rel="noreferrer">SQL Server 2005</a>, <a href="http://msdn.microsoft.com/en-us/library/ms173760%28v=sql.100%29.aspx" rel="noreferrer">SQL Server 2008</a>, <a href="http://msdn.microsoft.com/en-us/library/ms173760.aspx" rel="noreferrer">SQL Server 2012</a></p> <p>Here is another link <a href="http://itknowledgeexchange.techtarget.com/sql-server/why-is-my-select-count-running-so-slow/" rel="noreferrer">Why is my SELECT COUNT(*) running so slow?</a> with another solution. It shows technique that Microsoft uses to quickly display the number of rows when you right click on the table and select properties.</p> <pre><code>select sum (spart.rows) from sys.partitions spart where spart.object_id = object_id(’YourTable’) and spart.index_id &lt; 2 </code></pre> <p>You should find that this returns very quickly no matter how many tables you have.</p> <p>If you are using SQL 2000 still you can use the sysindexes table to get the number.</p> <pre><code>select max(ROWS) from sysindexes where id = object_id(’YourTable’) </code></pre> <p>This number may be slightly off depending on how often SQL updates the sysindexes table, but it’s usually corrent (or at least close enough).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload