?
Solved

is there any advantage of doing select count(*) from table instead of select col

Posted on 2011-09-27
5
Medium Priority
?
359 Views
Last Modified: 2012-05-12
hello,
I need to check for the existence of a dir in a table

is there any advantage/disadvantage of doing
select count(dir) from table where dir = 'dir'
vs
select dir from table where dir = 'dir'

Thanks
0
Comment
Question by:paries
[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
5 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36714455
Yes. select count(dir) from table where dir = 'dir' returns only the count of the dir column.

You can fine tune as below. This will be even more faster. It will select only one row even more rows exist in the table.

select TOP 1 from table where dir = 'dir'



0
 
LVL 15

Expert Comment

by:Eyal
ID: 36714459
if exists(select top 1 dir from table where dir = 'dir') is the right way
0
 

Author Comment

by:paries
ID: 36714468
EaswaranP:

select TOP 1 from mke_users where dir = 'dir'
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 from mke_users where dir = 'dir'' at line 1

0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36714477
Sorry I missed out the column name there.. That should be as select TOP 1 dir from mke_users where dir = 'dir'
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 2000 total points
ID: 36714655
if you want to check whether any record exists or not

try this

select dir from table where dir = 'dir' limit 1

this will return one row if record exists otherwise no rows
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 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