I have the following 2 mysql_queries
:
Query 1 (this query is repeated twice more for imgClass
, and imgGender
):
$imgFamily_query = "SELECT DISTINCT imgFamily FROM primary_images WHERE '$clause' ";
Query 2:
$query_pag_data = "SELECT imgId, imgURL, imgTitle, view, secondary FROM primary_images WHERE '$clause' ORDER BY imgDate DESC";
As you can see, the WHERE
is controlled by a variable. This variable is calculated as follows:
$where_clauses = array();
if ($imgFamilyFalse && $imgClassFalse && $imgGenderFalse) {
$where_clauses[] = "1=1"; // default do-nothing clause
}
if ($imgFamilyTrue) {
$where_clauses[] = 'imgFamily=' . "'" . mysql_real_escape_string($_GET['imgFamily']) . "'";
}
if ($imgClassTrue) {
$where_clauses[] = 'imgClass=' . "'" . mysql_real_escape_string($_GET['imgClass']) . "'";
}
if ($imgGenderTrue) {
$where_clauses[] = 'imgGender=' . "'" . mysql_real_escape_string($_GET['imgGender']) . "'";
}
$clause = implode(' AND ', $where_clauses);
The WHERE
clause is only dependant upon the following 3 columns:
imgFamily
imgClass
imgGender
However, depending upon the situation, a combination of any 1, 2, or 3 of those columns are utilized.
My question is, how should I go about setting up the indexes for primary_images
in this situation? It is a 'read-only' table, so I'm not concerned about having too many indexes. I would like the table to be as efficient in its querying as possible.
I was thinking about using a Multiple Column Index, but because the first column in the Multiple Column Index may not be present, the Index would not work.
Is it possible to set up several Multiple Column Indexes? Or would it be better in this case to just place an index on each of the 3 columns in question?