Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a very simple query:

SELECT COUNT(*)
FROM messages
INNER JOIN users ON messages.user_id = users.user_id

With the join it takes 1146 ms and without it takes 220 ms (220 ms still seems slow to me). Test carried out on the messages table containing 1,000,000+ rows.

I have I have a primary key set on both tables (message_id and user_id) and a foreign key setup connecting messages.user_id and users.user_id.

The reason for this query is to provide the total number of records for a paging system.

What else can I do to speed up the query?

share|improve this question

migrated from stackoverflow.com Aug 13 '12 at 15:36

This question came from our site for professional and enthusiast programmers.

2  
Do you have any rows in messages where user_id won't be in users? (e.g. is the foreign key nullable) –  Aaron Bertrand Aug 13 '12 at 15:31
    
No there will always be a match –  Darthtong Aug 13 '12 at 15:32
    
Please provide the CREATE TABLE statements including indexes and the execution plan. –  Martin Smith Aug 13 '12 at 15:34
1  
I don't see the point of the join, unless you plan to add filtering on the users table (an index on messages.user_id should help then would ) By now, it seems that a simple select count(message_id) from messages should do the trick –  Jean-Bernard Lagorce Aug 13 '12 at 15:46
add comment

3 Answers 3

up vote 4 down vote accepted

If the foreign key is not nullable, then there is no reason for the join at all, is there? So why not let SQL Server scan the most efficient index available instead of computing a join that will result in the exact same count?

SELECT COUNT(*) FROM dbo.messages;

That said, you can do this much more efficiently:

SELECT SUM(rows) FROM sys.partitions
  WHERE [object_id] = OBJECT_ID('dbo.messages')
  AND index_id IN (0,1);

This will be slightly inaccurate due to in-flight transactions, but it also won't block (or be blocked by) any activity against the table, so pick your poison.

share|improve this answer
1  
If the FK is trusted and the column doesn't allow NULLs I would have thought SQL Server will optimise the join out. –  Martin Smith Aug 13 '12 at 15:36
1  
@MartinSmith it may, I don't know that it's always guaranteed, but in any case the join code is still unnecessarily complex. The point of my answer was more about potentially trusting the catalog views to be "close enough" but orders of magnitude more efficient. –  Aaron Bertrand Aug 13 '12 at 15:38
    
A worked example on SO of the performance difference of COUNT vs DMVs stackoverflow.com/a/6069288/27535 –  gbn Aug 14 '12 at 7:36
add comment

You should create an index. An index can be created in a table to find data more quickly and efficiently.

The users cannot see the indexes, they are just used to speed up searches/queries.

CREATE INDEX index_name
ON table_name (column_name)

You should only create indexes on columns (and tables) that will be frequently searched against.

share|improve this answer
add comment

You're counting different things, from more data sources. Of course it's going to be slower.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.