Solved

MYSQL where in Query takes too long

Posted on 2012-03-29
4
390 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
[X]
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
  • 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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