0
我有以下PHP代碼從表單獲取數據,以及需要15分鐘才能完成的MySQL查詢。它需要更快。我想知道我是否忽略了使用PHP變量和MySQL查詢的任何最佳實踐,以及/或者查詢結構本身是否負責完成所需的不可接受的時間。PHP變量和非常緩慢的MySQL查詢
查詢的基本目的是統計選民PK出現的投票次數FK,如果計數高於預定義的數字,則從選民表中選擇相應的記錄(基於計算的百分比在最終的PHP條件語句上完成,以及在2004年以後過濾掉所有內容的查詢部分)。
選民表有100萬行,而選票表有700萬,兩個縣EMDSID鍵索引,但我不允許改變表結構,數據將永遠只能從他們讀取,永遠不會寫至。任何有關如何加快速度的建議或建議將不勝感激!
而且,遺憾的作文:P
/* ========== GET VALUES ========== */
if (isset($_GET["StreetName"])) { $searchStreetName = $_GET["StreetName"]; } else { $searchStreetName = "%"; }
if (isset($_GET["City"])) { $searchCity = $_GET["City"]; } else { $searchCity = "%"; }
if (isset($_GET["Zip"])) { $searchZip = $_GET["Zip"]; } else { $searchZip = "%"; }
if (isset($_GET["DOBY"])) { $searchDOBY = $_GET["DOBY"]; } else { $searchDOBY = "%"; }
if (isset($_GET["DOBM"])) { $searchDOBM = $_GET["DOBM"]; } else { $searchDOBM = "%"; }
if (isset($_GET["DOBD"])) { $searchDOBD = $_GET["DOBD"]; } else { $searchDOBD = "%"; }
if (isset($_GET["Gender"])) { $searchGender = $_GET["Gender"]; } else { $searchGender = "%"; }
if (isset($_GET["Party"])) { $searchParty = $_GET["Party"]; } else { $searchParty = "%"; }
if (isset($_GET["ED"])) { $searchED = $_GET["ED"]; } else { $searchED = "%"; }
if (isset($_GET["AD"])) { $searchAD = $_GET["AD"]; } else { $searchAD = "%"; }
if (isset($_GET["CD"])) { $searchCD = $_GET["CD"]; } else { $searchCD = "%"; }
if (isset($_GET["CO"])) { $searchCO = $_GET["CO"]; } else { $searchCO = "%"; }
if (isset($_GET["SD"])) { $searchSD = $_GET["SD"]; } else { $searchSD = "%"; }
if (isset($_GET["CC"])) { $searchCC = $_GET["CC"]; } else { $searchCC = "%"; }
if (isset($_GET["VoterActivity"])) { $searchVoterActivity = (($_GET["VoterActivity"]/100) * 18); } else { $searchVoterActivity = "0"; }
/* ========== GET DATA ========== */
$sql = "SELECT voters.*, COUNT(votes.CountyEMSID) AS 'activity'
FROM voters INNER JOIN votes ON voters.CountyEMSID = votes.CountyEMSID
WHERE voters.StreetName LIKE '$searchStreetName%'
AND voters.City LIKE '$searchCity%'
AND voters.Zip LIKE '$searchZip%'
AND voters.DOBY LIKE '%$searchDOBY'
AND voters.DOBM LIKE '%$searchDOBM'
AND voters.DOBD LIKE '%$searchDOBD'
AND voters.Gender LIKE '$searchGender%'
AND voters.Party LIKE '$searchParty%'
AND voters.ED LIKE '%$searchED'
AND voters.AD LIKE '%$searchAD'
AND voters.CD LIKE '%$searchCD'
AND voters.CO LIKE '%$searchCO'
AND voters.SD LIKE '%$searchSD'
AND voters.CC LIKE '%$searchCC'
AND votes.ElectionDateY >= 2004
AND (
votes.ElectionType = 'GE'
OR votes.ElectionType = 'PR'
OR votes.ElectionType = 'PP'
)
GROUP BY votes.CountyEMSID
HAVING COUNT(votes.CountyEMSID) >= '$searchVoterActivity'
ORDER BY voters.LastName, voters.FirstName, voters.DOBY, voters.DOBM, voters.DOBD ASC
LIMIT $start, $limit";
/* ========== CREATE TABLE ========== */
CREATE TABLE voters (
CountyEMSID varchar(9) NOT NULL,
LastName varchar(30) NOT NULL,
FirstName varchar(30) NOT NULL,
MiddleInitial varchar(1) NOT NULL,
NameSuffix varchar(4) NOT NULL,
HouseNumber varchar(10) NOT NULL,
HouseNumberSuffix varchar(10) NOT NULL,
ApartmentNumber varchar(15) NOT NULL,
StreetName varchar(50) NOT NULL,
City varchar(40) NOT NULL,
Zip varchar(5) NOT NULL,
ZipCode4 varchar(4) NOT NULL,
MailingAddress1 varchar(50) NOT NULL,
MailingAddress2 varchar(50) NOT NULL,
MailingAddress3 varchar(50) NOT NULL,
MailingAddress4 varchar(50) NOT NULL,
DOBY varchar(4) NOT NULL,
DOBM varchar(2) NOT NULL,
DOBD varchar(2) NOT NULL,
Gender varchar(1) NOT NULL,
Party varchar(3) NOT NULL,
Other varchar(30) NOT NULL,
ED varchar(3) NOT NULL,
AD varchar(2) NOT NULL,
CD varchar(2) NOT NULL,
CO varchar(2) NOT NULL,
SD varchar(2) NOT NULL,
CC varchar(2) NOT NULL,
RegY varchar(4) NOT NULL,
RegM varchar(2) NOT NULL,
RegD varchar(2) NOT NULL,
Status varchar(2) NOT NULL,
VoterType varchar(1) NOT NULL,
StatusChangeY varchar(4) NOT NULL,
StatusChangeM varchar(2) NOT NULL,
StatusChangeD varchar(2) NOT NULL,
LastVoted varchar(4) NOT NULL,
Telephone varchar(12) NOT NULL,
KEY CountyEMSID (CountyEMSID)
)
/* ========== CREATE TABLE ========== */
CREATE TABLE votes (
CountyEMSID varchar(9) NOT NULL,
County varchar(2) NOT NULL,
AD varchar(2) NOT NULL,
ED varchar(3) NOT NULL,
Party varchar(3) NOT NULL,
ElectionDateY varchar(4) NOT NULL,
ElectionDateM varchar(2) NOT NULL,
ElectionDateD varchar(2) NOT NULL,
ElectionType varchar(2) NOT NULL,
VoterType varchar(1) NOT NULL,
KEY CountyEMSID (CountyEMSID)
)
在我的mysql IRC頻道上提到mysql一次只能使用一個索引,而我最好的選擇是使用複合索引,從左到右按我搜索的順序。但是,我不明白這將是多麼有效,這將是我正在使用的變量的數量。有沒有我不瞭解如何建立索引應用於這種情況? 編輯:我從我的架構轉儲刪除索引代碼,以減少代碼混亂,如我的帖子中所述,兩個縣EMSS鍵索引。 – deathonater
'CountyEMSID'是你的主鍵,這是一個非常基本的索引是。但是,雖然MySQL一次只使用一個索引,但索引可以像你想的那樣複雜。在多個鍵上執行復合索引以加快查詢速度。 – buley
感謝您的建議!我會嘗試在3個或4個最常搜索的列上進行復合索引。 – deathonater