Solved

MYSQL where in Query takes too long

Posted on 2012-03-29
4
386 Views
Last Modified: 2012-03-29
Hello,

I am trying to run a query to determine how many users I have in each state.

I have a table with all the zips in the USA and a table with all my users with a field for zip code:

So I run the following query:

SELECT COUNT( * )
FROM profile_fields_data
WHERE pf_zip_code
IN (

SELECT zip_code
FROM zips
WHERE state =  "CA"
)

My profile fields table has 4200 records and my zipcode table has 33,000 records and when i run this query it takes 51 seconds which is absurd!!!

If I run just this query
SELECT zip_code
FROM zips
WHERE state =  "CA"  

It takes milliseconds - how can I fix this, as I plan to run 50 queries to list out all 50 states.
0
Comment
Question by:neilsav
  • 2
  • 2
4 Comments
 
LVL 24

Expert Comment

by:johanntagle
ID: 37785544
Make sure  profile_fields_data.pf_zip_code and zips.zip_code and zips.state are indexed.  Then use a join instead of an IN subquery:

SELECT COUNT( p.* )
FROM profile_fields_data p
JOIN zips z
ON p.pf_zip_code=z.zip_code
WHERE z.state='CA';

By the way, especially if your tables are of innodb, it's better to count(p.primary_column) than p.*
0
 
LVL 1

Author Comment

by:neilsav
ID: 37785561
Using a join doesn't help me that just returns the amount of matches in the two tables.

I need to know what zipcodes are from particular state that is why i am using where in
0
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 37785568
Your original query just did a count so that's what I rewrote to a join.  Okay so I re-read your question and you said at the top that what you really want is to know how many users are on each state.  So it can be like this:

SELECT z.state, COUNT( p.* )
FROM zips z  
JOIN profile_fields_data p
ON p.pf_zip_code=z.zip_code
group by z.state;
0
 
LVL 1

Author Closing Comment

by:neilsav
ID: 37785572
Perfect!  Thanks!
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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 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

17 Experts available now in Live!

Get 1:1 Help Now