I need to know where to put indexes
in my queries. I am at lost as how to use it although I am searching about this for quite some time now.
Here are my queries: (in PHP)
1) In operator
, I do use >
and =
. I use =
often.
SELECT {columns} FROM {table}
WHERE {field} {operator} {value}
2) This query is for pagination, limit
is always equal to 10.
SELECT {columns} FROM {table}
WHERE {field} {operator} {value}
ORDER BY date DESC LIMIT {start}, {limit}
3) This query is for checking the sent and received emails to and from other users.
SELECT {columns} FROM {table} alias1
WHERE (alias1.{field} = {value} or alias1.{field2} = {value})
AND EXISTS (SELECT max(alias2.date) alias_date FROM {table} alias2
WHERE alias1.{field} = alias2.{field}
and alias1.{field2} = alias2.{field2}
GROUP BY {field}, {field2}
HAVING alias1.date = alias_date)
ORDER BY alias1.date DESC
4) A query using IN
.
SELECT {columns} FROM {table}
WHERE {field} IN ({in_data})
ORDER BY FIELD ({field}, {in_data})
5) Query using ASC
. Data in IN
are numeric.
SELECT {columns} FROM {table}
WHERE {field} IN ('in1', 'in2')
ORDER BY date ASC
6) Query using AND
.
SELECT {columns} FROM {table}
WHERE {field} {operator} {value}
AND {field2} {operator2} {value2}
7) Query with DESC
.
SELECT {columns} FROM {table}
WHERE {field} {operator} {value}
ORDER BY date DESC
In where column/s do I need to use index
? The important column//s that in need of index
? I need some help.
Note
The {field}
and {field2}
are only one field, e.g. WHERE {lastname} = {value}
.
Update
I posted a more detailed query from my codes and how to implement index
based on how I understand it. Here is the link: Are the index here will suffice to make the query faster?
field
is only one field, only #4 and #6 have multiplefield
:field
andfield2
. And what do you mean by Note, that if you index just the field(s) in your where clause you might still need to add columns for those in your select., I am not that good in english so I do not understand what you meant in your last sentence.