Solved

SELECT COUNT vs SELECT ... LIMIT

Posted on 2004-04-03
6
2,624 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Read about achieving the basic levels of HRIS security in the workplace.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now