簡體   English   中英

無法進一步優化MySQL查詢:我缺少什么?

[英]Unable to optimise MySQL query further: what am I missing?

我有一個查詢似乎無法進一步優化(關於執行時間)。 這是一個簡單的簡單查詢,索引到位,我已經嘗試配置InnoDB設置......但似乎沒有任何幫助。

查詢是三個表trk,auf和paf之間的JOIN。

  • trk:臨時表,表示id代表曲目。
  • auf:表示與曲目相關的音頻文件的表。
  • paf:包含已發布音頻文件的id的表。 充當“過濾器”。
// 'trk' table  
CREATE TEMPORARY TABLE auf_713340 (  
  `id` char(36),   
  PRIMARY KEY (id)  
) ENGINE=MEMORY);  

// 'auf' table  
CREATE TABLE `file` (  
 `id` char(36) NOT NULL,  
 `track_id` char(36) NOT NULL,  
 `type` varchar(3) DEFAULT NULL,  
 `quality` int(1) DEFAULT '0',  
 `size` int(20) DEFAULT '0',  
 `duration` float DEFAULT '0',  
 `bitrate` int(6) DEFAULT '0',  
 `samplerate` int(5) DEFAULT '0',  
 `tagwritten` datetime DEFAULT NULL,  
 `tagwriteattempts` int(3) NOT NULL DEFAULT '0',  
 `audiodataread` datetime DEFAULT NULL,  
 `audiodatareadattempts` int(3) NOT NULL DEFAULT '0',  
 `converted` datetime DEFAULT NULL,  
 `convertattempts` int(3) NOT NULL DEFAULT '0',  
 `waveformgenerated` datetime DEFAULT NULL,  
 `waveformgenerationattempts` int(3) NOT NULL DEFAULT '0',  
 `flag` int(1) NOT NULL DEFAULT '0',  
 `status` int(1) NOT NULL DEFAULT '0',  
 `updated` datetime NOT NULL DEFAULT '2000-01-01 00:00:00',  
 PRIMARY KEY (`id`),  
 KEY `FK_file_track` (`track_id`),  
 KEY `file_size` (`size`),  
 KEY `file_type` (`type`),  
 KEY `file_quality` (`quality`),  
 CONSTRAINT `file_ibfk_1` FOREIGN KEY (`track_id`) REFERENCES `track` (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8

// 'paf' table  
CREATE TABLE `publishedfile` (  
  `file_id` varchar(36) NOT NULL,  
  `data` varchar(255) DEFAULT NULL,  
  `file_updated` datetime NOT NULL,  
  PRIMARY KEY (`file_id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8  

查詢通常需要1500毫秒到2500毫秒才能在trk表中的50到100個ID之間執行.auf表包含大約110萬行,而paf表包含大約900.000行。

MySQL服務器在4GB Rackspace Cloud Server實例上運行。

查詢

SELECT auf.*
FROM auf_713340 trk
INNER JOIN file auf
  ON auf.track_id = trk.id
INNER JOIN publishedfile paf
  ON auf.id = paf.file_id

查詢帶有EXPLAIN

id select_type table type   possible_keys         key           key_len ref                                 rows Extra
1  SIMPLE      trk   ALL    NULL                  NULL NULL     NULL                                        60  
1  SIMPLE      auf   ref    PRIMARY,FK_file_track FK_file_track 108     func                                1   Using where
1  SIMPLE      paf   eq_ref PRIMARY               PRIMARY       110     trackerdatabase_development.auf.id  1   Using where; Using index

InnoDB配置

[mysqld]

# The size of memory used to cache table data and indexes. The larger 
# this value is, the less I/O is needed to access data in tables. 
# Default value is 8MB. Recommendations point towards 70% - 80% of 
# available system memory.
innodb_buffer_pool_size=2850M

# Recommendations point towards using O_DIRECT to avoid double buffering.
# innodb_flush_method=O_DIRECT

# Recommendations point towards using 256M.
# @see http://www.mysqlperformanceblog.com/2006/07/03/choosing-proper-innodb_log_file_size/
innodb_log_file_size=256M

# The size in bytes of the buffer that InnoDB uses to write to the log files
# on disk. Recommendations point towards using 4MB.
innodb_log_buffer_size=4M

# The size of the buffer used for MyISAM index blocks.
key_buffer_size=128M

現在,問題是; 我該怎么做才能讓查詢更好地執行? 畢竟,有問題的表並沒有那么大,索引到位了。?

在auf表中,將id字段設為int(11)並使其自動遞增。 所有int字段長度> 11,編輯為11。

謝謝Ripa Saha

嘗試這個:

SELECT auf.* 
FROM file auf 
WHERE EXISTS 
      ( SELECT *
        FROM auf_713340 trk 
        WHERE auf.track_id = trk.id 
      )
  AND EXISTS
      ( SELECT *
        FROM publishedfile paf
        WHERE auf.id = paf.file_id
      ) ;

我還將測試和比較效率與使用InnoDB引擎定義的臨時表或使用(主)索引作為BTREE索引。 內存表默認有HASH索引,如果我沒記錯的話,不是Btree。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM