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
Solved

MYSQL where in Query takes too long

Posted on 2012-03-29
4
388 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Suggested Solutions

Title # Comments Views Activity
Optimize the query 5 42
jQuery Toggle & Anchor Links 5 42
How does PHP Storm display on Linux high resolution laptops? 1 36
Wordpress Only run code if on a certain page 11 22
Creating and Managing Databases with phpMyAdmin in cPanel.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

790 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