Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-27
5
Medium Priority
?
363 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
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
Course of the Month10 days, 5 hours left to enroll

926 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