views:

195

answers:

4

I run this query:

SELECT u.user_id, u.fname, u.lname, n.title, n.news_id, n.post, 
n.zip, z.city,z.state_abbr
FROM yc_users u, yc_news n, yc_zipcodes z
WHERE u.user_id = n.user_id AND n.zip = z.zip
ORDER BY n.stamp
LIMIT 10

And get this error:

The SELECT would examine more than MAX_JOIN_SIZE rows; 
check your WHERE and use SET SQL_BIG_SELECTS=1 or 
SET SQL_MAX_JOIN_SIZE=# if the SELECT is okay

I've got over 42,000 rows under yc_zipcodes. The other tables hold less than 10 rows at the moment.

EDIT: Data samples as requested:

yc_zipcodes

zip   city       state_abbr
00210   Portsmouth NH
00211   Portsmouth NH
00212   Portsmouth NH
00213   Portsmouth NH

yc_users

user_id  username  password                         fname  lname     email            zip  active_bln
1          fission1    e09dc84a23fd6cd68ce1fff1ff95713a   Hayden   Ferguson   [email protected]  92831 1
2          jason    c2d0d212936c4bfd7f587607e6c72808   jason    stevenson  [email protected]   93710 1

yc_news

news_id user_id   title                         post                                         zip    stamp  active_bln
2      1      Gummy bear falls into manhole   OMG! A drunk man dressed as gummy  bear...   93740 2009-10-12 09:49:04 1
3      1      Guy robbed                       Some dude got robbed last night at corner of... 93740 2009-10-12 09:50:19 1

The data above is dud. No gummy bears were during the making of this application =D

+3  A: 

You should use a JOIN, and not just select from all tables. If you select from all tables, all possible combinations of rows are generated (and this are A LOT) and then the WHERE filters out unneeded rows.

Use this, for example:

SELECT       u.user_id, 
             u.fname, 
             u.lname, 
             n.title, 
             n.news_id, 
             n.post, 
             n.zip, 
             z.city,
             z.state_abbr
FROM         yc_users u
INNER JOIN   yc_news n 
ON           u.user_id = n.user_id
INNER JOIN   yc_zipcodes z
ON           n.zip = z.zip
ORDER BY     n.stamp
LIMIT        10

EDIT:

I can't see any obvious problems in your query. I would just set the options as the error message tells you and then look if the result is the one you wanted to get. If it is - fine. If it isn't - come back and tell us.

Maximilian Mayerl
I've only just started working with database queries. I don't really know how to use JOINS. I've read through some tutorials but I'm still confused. None of my queries seem to work.Can you please provide an example?
gAMBOOKa
Weird... getting the exact same error!
gAMBOOKa
Are you sure you have only 42.000 rows in your table?
Maximilian Mayerl
42,192 to be exact
gAMBOOKa
Could you please provide some sample data? For example, the data in all the tables which is related to a single user in yc_users?
Maximilian Mayerl
Sample data added
gAMBOOKa
What are the record counts of the other tables (yc_users and yc_news) involved? Because the default value of max_join_size appears to be 4,294,967,295. See: http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_join_size
Cloud
Under 20 each right now. But could potentially reach a lot more.
gAMBOOKa
The way his query is written IS an inner join in Mysql. INNER JOIN is implied by use of comma (,) delimiting tables. See http://dev.mysql.com/doc/refman/5.0/en/join.html
Kevin Peno
A: 

Your WHERE clause is equivalent to INNER JOINs (although I generally prefer explicit JOINs) and appears to join properly and not result in an inadvertant CROSS JOIN.

What version of MySQL are you using?

Cade Roux
MySQL Version 5.0.77. Using phpMyAdmin.
gAMBOOKa
+2  A: 

You should aim to reduce the size of the result set - ensure the join(s) will filter records needed sooner; choosing indexes more carefully should help with this.

  • Do you have indexes (at least) on either yc_users.id or yc_news.id and yc_zipcodes.zip?
  • Try running the query without the ORDER BY and see if it makes a difference

See also these related StackOverflow threads:

More details about max_join_size

Adrian
+1 for other references
Kevin Peno
What do indexes have to do with a MAX_JOIN_SIZE error?
Maximilian Mayerl
A: 

While the query could be a little more optimized (depending on what you are trying to accomplish), i do not see how it can be changed to a point which would not trigger the error as the information is presented. As the accepted answer here states, you are probably ok with the joins and setting SQL_BIG_SELECTS=1.

However, that being said, I would evaluate the necessity of the joins you are making and probably open another question with more information on exactly what you are trying to accomplish with the data you are querying.

From first glance I can assume that you are grabbing all articles a user made and where they are from. If this is the case, I would LEFT JOIN news to user and get the zip relationship in another query.

Kevin Peno