[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SELECT COUNT vs SELECT ... LIMIT

Posted on 2004-04-03
6
Medium Priority
?
2,700 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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