I'm not sure if you have resolved the problem or not, but here's some test data that I have produced. There are a number of factors that can affect the speed of your queries, so my simple test cases may not accurately reflect your tables or data. However, they serve as a useful starting point.
First, create 5 simple tables, each with the same structure. As with your tables, I have used a UNIQUE
index on the url
column:
CREATE TABLE `table1` (
`id` int(11) NOT NULL auto_increment,
`url` varchar(255) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`)
) ENGINE=InnoDB;
CREATE TABLE table2 LIKE table1;
CREATE TABLE table3 LIKE table1;
CREATE TABLE table4 LIKE table1;
CREATE TABLE table5 LIKE table1;
The following script creates a stored procedure which is used to fill each table with 10,000 rows of data:
DELIMITER //
DROP PROCEDURE IF EXISTS test.autofill//
CREATE PROCEDURE test.autofill()
BEGIN
DECLARE i INT DEFAULT 5;
WHILE i < 10000 DO
INSERT INTO table1 (url) VALUES (CONCAT('wwww.stackoverflow.com/', i ));
INSERT INTO table2 (url) VALUES (CONCAT('wwww.stackoverflow.com/', 10000 - i ));
INSERT INTO table3 (url) VALUES (CONCAT('wwww.stackoverflow.com/', i + 6000 ));
INSERT INTO table4 (url) VALUES (CONCAT('wwww.stackoverflow.com/', i + 3000 ));
INSERT INTO table5 (url) VALUES (CONCAT('wwww.stackoverflow.com/', i + 2000 ));
SET i = i + 1;
END WHILE;
END;
//
DELIMITER ;
CALL test.autofill();
Each table now contains 10,000 rows. Your SELECT
statement can now be used to query the data:
SELECT *
FROM table1,table2,table3,table4,table5
WHERE table1.url = table2.url
AND table1.url = table3.url
AND table1.url = table4.url
AND table1.url = table5.url
AND table1.url = 'wwww.stackoverflow.com/8000';
This gives the following result almost instantly:
+------+-----------------------------+------+-----------------------------+------+-----------------------------+------+-----------------------------+------+-----------------------------+
| id | url | id | url | id | url | id | url | id | url |
+------+-----------------------------+------+-----------------------------+------+-----------------------------+------+-----------------------------+------+-----------------------------+
| 7996 | wwww.stackoverflow.com/8000 | 1996 | wwww.stackoverflow.com/8000 | 1996 | wwww.stackoverflow.com/8000 | 4996 | wwww.stackoverflow.com/8000 | 5996 | wwww.stackoverflow.com/8000 |
+------+-----------------------------+------+-----------------------------+------+-----------------------------+------+-----------------------------+------+-----------------------------+
An EXPLAIN SELECT
shows why the query is very fast:
EXPLAIN SELECT *
FROM table1,table2,table3,table4,table5
WHERE table1.url = table2.url
AND table1.url = table3.url
AND table1.url = table4.url
AND table1.url = table5.url
AND table1.url = 'wwww.stackoverflow.com/8000';
+----+-------------+--------+-------+---------------+------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+------+---------+-------+------+-------------+
| 1 | SIMPLE | table1 | const | url | url | 258 | const | 1 | Using index |
| 1 | SIMPLE | table2 | const | url | url | 258 | const | 1 | Using index |
| 1 | SIMPLE | table3 | const | url | url | 258 | const | 1 | Using index |
| 1 | SIMPLE | table4 | const | url | url | 258 | const | 1 | Using index |
| 1 | SIMPLE | table5 | const | url | url | 258 | const | 1 | Using index |
+----+-------------+--------+-------+---------------+------+---------+-------+------+-------------+
select_type
is SIMPLE
, which means that there are no JOIN
statements to slow things down.
type
is const
, which means that the table has at most one possible match - this is thanks to the UNIQUE
index, which guarantees no two URLs will be the same (see mysql 5.0 indexes - Unique vs Non Unique for a good description of UNIQUE INDEX
). A const
value in the type
column is about as good as you can get.
possible_keys
and key
use the url
key. That means that the correct index is being used for each table.
ref
is const
, which means that MySQL is comparing a constant value (one that does not change) with the index. Again, this is very fast.
rows
equals 1. MySQL only needs to look at one row from each table. Once again, this is very fast.
Extra
is Using index
. MySQL does not have to do any additional non-indexed searches of the tables.
Provided you have an index on the url
column of each table, your query should be extremely fast.