Solved

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

Posted on 2011-09-27
5
353 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
5 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
Comment Utility
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
Comment Utility
if exists(select top 1 dir from table where dir = 'dir') is the right way
0
 

Author Comment

by:paries
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

14 Experts available now in Live!

Get 1:1 Help Now