If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to join tables with same structure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-25-09, 12:17
fopgames fopgames is offline
Registered User
 
Join Date: Jul 2009
Posts: 4
Cool How to join tables with same structure

I have multiple tables, table_a, table_b, table_c. Each having the same structure. i.e ID, title, rating, content. I'm using

(select * from table_a order by rating DESC) union (select * from table_b order by rating DESC) union (select * from table_c order by rating DESC) to list all titles from a,b,c tables. But it only lists tables one after the other.

How can I list top ten rated titles from all the tables.
Reply With Quote
  #2 (permalink)  
Old 07-25-09, 12:31
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Code:
SELECT *
FROM 
( 
  SELECT * 
  FROM table_a
  UNION ALL
  SELECT * 
  FROM table_b
  UNION ALL
  SELECT * 
  FROM table_c
) t
ORDER BY rating DESC
LIMIT 10
But you should really re-think your table design.
A lot of tables with the same base name, but with a different incrementing suffix that all have the same structure sure sounds like bad design.
Your current problem is just one proof that this is not right
Reply With Quote
  #3 (permalink)  
Old 07-25-09, 12:40
fopgames fopgames is offline
Registered User
 
Join Date: Jul 2009
Posts: 4
Thank you very much, I'll try out your suggestion. I have more than 500,000 items arranged into different tables alphabetically. It might be too heavy to fill all of 'em in a single table. And even the query would be too slow. What do you suggest?
Reply With Quote
  #4 (permalink)  
Old 07-25-09, 12:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by fopgames
What do you suggest?
you should use one table only, and learn about indexing

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-25-09, 12:53
fopgames fopgames is offline
Registered User
 
Join Date: Jul 2009
Posts: 4
Thanks Rudy, I'll consider that.

Last edited by fopgames; 07-25-09 at 12:56.
Reply With Quote
  #6 (permalink)  
Old 07-25-09, 13:10
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by fopgames
I have more than 500,000 items arranged into different tables alphabetically. It might be too heavy to fill all of 'em in a single table. And even the query would be too slow.
No. A database that cannot handle 500,000 rows in a single table is not worth to be called a database.
MySQL can handle a lot more than that.
500,000 rows can't even be called a "big" table
Reply With Quote
  #7 (permalink)  
Old 07-25-09, 13:33
fopgames fopgames is offline
Registered User
 
Join Date: Jul 2009
Posts: 4
But won't that make queries slower?
Reply With Quote
  #8 (permalink)  
Old 07-25-09, 13:44
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by fopgames
But won't that make queries slower?
Not with proper indexing.

The query we are talking about (that includes all tables) will most probably even be faster.

You should not jeopardize your DB design because you think it could be slower.

First get your design correct, then run your queries, then check the slow ones and try to tune them.

If you can't tune a specific query so that the performance meets your needs then - and only then - you might think about moving from a good design to a bad design.
Reply With Quote
  #9 (permalink)  
Old 07-27-09, 08:28
BCB BCB is offline
Registered User
 
Join Date: May 2009
Posts: 2
In 5.1 you can use table partitioning with the indexes for even better performance. See the manual for more detail.
Reply With Quote
  #10 (permalink)  
Old 07-27-09, 09:45
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by BCB
In 5.1 you can use table partitioning with the indexes for even better performance. See the manual for more detail.
For only 500.000 rows no partitioning should be needed.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On