我的表:BIG_TABLEmysql連接:什麼更快?
+-----------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+---------+----------------+ | id | mediumint(7) | NO | PRI | NULL | auto_increment | | title | varchar(255) | NO | | NULL | | | category_id | tinyint(2) | NO | | NULL | | | sub_category_id | tinyint(2) | NO | | NULL | | | width | smallint(5) | NO | | NULL | | | height | smallint(5) | NO | | NULL | | | ratio_width | smallint(5) | NO | | NULL | | | ratio_height | smallint(5) | NO | | NULL | | | size | int(8) | NO | | NULL | | | mime | tinyint(2) | NO | | NULL | | | views | mediumint(7) | NO | MUL | NULL | | | time | int(10) | NO | | NULL | | | file | varchar(255) | NO | | NULL | | +-----------------+--------------+------+-----+---------+----------------+
small_table
+--------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------+------+-----+---------+-------+ | id | mediumint(7) | NO | PRI | NULL | | | width | smallint(5) | NO | MUL | NULL | | | height | smallint(5) | NO | MUL | NULL | | +--------+--------------+------+-----+---------+-------+
有啥更快(例如):
SELECT * FROM `big_table` WHERE `width` =1920 AND `height`=1080;
,或者使用加入
select big_table.*
from small_table
left join small_table small_table2
ON (small_table.id=small_table2.id
and `small_table`.`height` = '1080')
left join big_table
ON (big_table.id=small_table.id)
where small_table.width = '1920';
或加入的FR呃同一張桌子?
select big_table.*
from big_table as big_table1
left join big_table big_table2
ON (big_table1.id=big_table2.id and `big_table1`.`height` = '1080')
left join big_table
ON (big_table.id=big_table1.id)
where big_table1.width = '1920';
還是有一些更好的解決方案,更好的選擇? (在這兩個表上,我可以使用索引(寬度和高度),但只有ID是唯一的)
那你試圖完成什麼?它應該做什麼,結果是什麼? –
嘗試'$ sql ='...'; $ msc = microtime(true); $ mysql_query($ sql); $ msc = microtime(true) - $ msc; echo $ msc。'秒; echo($ msc * 1000)。'毫秒; ' –
首先會更快,因爲它只從一個表獲取數據 –