Solved

SELECT COUNT vs SELECT ... LIMIT

Posted on 2004-04-03
6
2,686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

728 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