| which is fastest: a join or one big table? [message #1284] |
Mon, 21 May 2007 22:43  |
bluem Messages: 15 Registered: November 2006 |
Junior Member |
|
|
I seem to be facing two possible scenarios. Which of these would offer the fastest SELECT?
Option 1) A master table that references 3 other tables via IDs. The nature of the SELECT will require that *every* row of the master table will be joined with a row from one or all of the 3 other tables. Any column in any affected table could be selectable, sortable or "countable" -- and so could benefit from an index; but will all the -joined- table indexes be usable?
Option 2) All data is in one big table already, with indexes on any selectable column, so virtually any query would be directly indexed; no joins are made. The number of columns has been multiplied greatly (and with much empty data) because I have essentially already joined everything myself and inserted the data into a big, fully indexed table. I wonder if this is better because MySQL need not create a temporary (joined) table.
Any advice is appreciated.
|
|
|
|
|
| Re: which is fastest: a join or one big table? [message #1291 is a reply to message #1287 ] |
Wed, 23 May 2007 04:04  |
sterin Messages: 323 Registered: March 2007 Location: Sweden |
Senior Member |
|
|
| bluem wrote on Tue, 22 May 2007 18:19 |
If we assume that both scenarios take place in RAM -- will a join always be slower than reading from one table?
|
Usually you don't have to think about it since if they are both in RAM then the amount of rows is small enough and since RAM is so very much faster than disks you don't really have the problem.
| Quote: | A table scan always took about 25 minutes (depending on load on system). While a query that used indexes might easily take about 8 hours due to disk seek times.
|
| bluem wrote on Tue, 22 May 2007 18:19 |
That's crazy - I thought indexes were supposed to speed up select queries? (How do you force a table scan?)
|
They are, but using an index means random access and if you are selecting just 1 row from a large table then it is a huge difference.
But if you are selecting all rows then using an index is just plain silly.
And somewhere between there you have a breaking point where it is more advantagous to just run a table scan.
For that perticular database that point was at about 1/20 of the rows.
And I forced it with a hint in the query but that perticular database was Oracle.
| bluem wrote on Tue, 22 May 2007 18:19 |
Final question: How do I know if mysql can hold everything in RAM? If I remember correctly, there are mysql settings for this.
|
You can check the size of your DB and check how much RAM you got available on the server.
If you are running MyISAM table types then the important variable is key_buffer_size.
If you are running InnoDB then innnodb_buffer_pool_size is the important.
Read about them in the manual:
http://dev.mysql.com/doc/refman/5.1/en/server-system-variabl es.html
http://dev.mysql.com/doc/refman/5.1/en/innodb-parameters.htm l
|
|
|