Solved

SELECT COUNT vs SELECT ... LIMIT

Posted on 2004-04-03
6
2,667 Views
Last Modified: 2010-05-18
Hi experts.

My doubt is quite general. I usually develope with SQL Server and My-SQL.

I want to know if there is ANY row that match the search. No matter in how many are there.

a) SELECT COUNT(colname1) FROM Foo WHERE colname2 = 'something' AND colname3 = 1;
b) SELECT colname1 FROM Foo WHERE colname2 = 'something' AND colname3 = 1 LIMIT 1;

Does first one need a full table scan? May be the second one faster, because it stops when the first match is found?

Thanks!
0
Comment
Question by:Eligio
6 Comments
 
LVL 1

Accepted Solution

by:
pfaires earned 43 total points
ID: 10751349
Hi,

You´re right. The first one will require a full table scan in order to count the matches. The second one will scan the table too, but will return after the first match, so it will be faster than the first one. Note that in any case, you can greatly increase the query speed by creating an appropriate index, to transform the full table scan into an index scan.

If you´re using MSSQL, just go to query analyser and select index analisys, so it will sugest the best index for you.

Regards,
Paulo.
0
 
LVL 8

Assisted Solution

by:Vikas_Dixit
Vikas_Dixit earned 41 total points
ID: 10751380
The full table scan will depend upon whether you have indices on colname2 and colname3..

This is how Oracle works with the rowid : If you have index then in first case, It will do a index range scan ( or unique access depending upon the condition) and will not at all go to the table, as not data from the table is needed, it's just the count that is required. If you Don't have an Index, then A full table scan will be done to return the count.
For Second case, If you have index, It will do a index range scan and, then access the table with the rowid and then return the first matched row.

So there's not going to be much difference in time taken by 1 & 2 if the table has an index on the coulmns, and the Index scan range is small. Otherwise, 2 will be faster.

Also, LIMIT in non standard, so it's not going to work when you switch databases..

--Vikas
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 41 total points
ID: 10751479
i think they both require a "table"/Index scan

all your really doing is limiting the number of output rows......

1) does really need to scan since you've asked it to physically count the rows...

2) show do a scan , because its an output decision as to which of the available rows it will "limit"   itself to return....

hth

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question