Solved

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

Posted on 2011-09-27
5
354 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

19 Experts available now in Live!

Get 1:1 Help Now