Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SELECT COUNT vs SELECT ... LIMIT

Posted on 2004-04-03
6
Medium Priority
?
2,715 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
3 Comments
 
LVL 1

Accepted Solution

by:
pfaires earned 172 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 164 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 164 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 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