我有一個模式MySQL的選擇降低性能類似的查詢
applicants - id, max_res_id, max_visa_id
applicant_files - id, applicatid, fileid, filetype
files - id, name, filetype
申請人 -
CREATE TABLE `applicants` (
`id` char(36) NOT NULL,
`max_res_id` char(36) NOT NULL,
`max_visa_id` char(36) NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_res_id` (`max_res_id`) USING BTREE,
KEY `idx_visa_id` (`max_visa_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
applicant_files
CREATE TABLE `applicant_files` (
`id` char(36) CHARACTER SET latin1 NOT NULL,
`applicantid` char(36) CHARACTER SET latin1 DEFAULT NULL,
`fileid` char(36) CHARACTER SET latin1 DEFAULT NULL,
`filetype` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `q_applicantfile_fileid` (`fileid`),
KEY `u_applicantfile_applid` (`applicantid`),
KEY `idx_filetype` (`filetype`) USING BTREE,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
文件
CREATE TABLE `files` (
`id` char(36) NOT NULL,
`filetype` tinyint(1) NOT NULL,
`name` text,
PRIMARY KEY (`id`),
KEY `idx_filetype` (`filetype`) USING BTREE,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
max_res_id, max_visa_id
referes到applicant_files
fileid
「ID」 是指files
現在 「身份證」 我有2個不同的查詢 -
select f.id as resumeId, f.name as resumeName, f.date_entered as resumeDate,
a.id as applId
from oepl_applicants a
inner join applicant_files af
ON (a.id in ('id1', 'id2')
and a.id = af.applicantid
and a.max_res_id = af.id
and af.filetype = 1
and a.max_res_id != ''
and a.max_res_id is not null)
inner join files f
ON (af.fileid = f.id
and f.filetype = 1)
select f.id as visaId, f.name as visaName, f.date_entered as visaDate,
a.id as applId
from oepl_applicants a
inner join applicant_files af
ON (a.id in ('id1', 'id2')
and a.id = af.applicantid
and a.max_visa_id = af.id
and af.filetype = 2
and a.max_visa_id != ''
and a.max_visa_id is not null)
inner join files f
ON (af.fileid = f.id
and f.filetype = 2)
200個IDS(ID1,ID2,... ID200) ,第一個查詢是在2秒內返回結果,而第二個查詢是在30秒內返回結果。
這裏可能會出現什麼問題?
這兩個查詢的唯一區別在於文件類型不同,連接在2個不同的列上。 PS - 與max_res_id中的值相比,max_visa_id中的很多值爲空(空)
爲什麼主鍵字符串值? ...爲什麼字符串值使用不同的字符集? – Uueerdo
你有列是yoru查詢,我沒有看到你的架構..(例如:f.deleted)是你確定這是正確的架構 – scaisEdge
@scaisEdge:這裏顯示的模式是一個縮小版本。我的表格中有很多其他列。爲了在此討論,請考慮每個表中都有一個「已刪除」標誌。 –