mysql different query share one index - mysql

I have 2 queries
SELECT * FROM table WHERE store_id=1 && album_id=1 && delete=0
UPDATE table SET delete=0 WHERE store_id=1 && album_id=1
I create an index store_id, album_id, delete
my question is can these 2 queries share this index?
or I have to create another index (store_id, album_id) for 2nd one

If you are creating one index that is a composite index as table(store_id, album_id, delete), then both queries should be able to use this index.
THe first will use all three fields in the index because they match the where clause exactly.
The second query will use the first two columns in the index for its where clause.

Related

MySQL - Does SELECT * need an index of all table fields?

I would like to know if it is necessary to create an index for all fields within a table if one of your queries will use SELECT *.
To explain, if we had a table that 10M records and we did a SELECT * query on it would the query run faster if we have created an index for all fields within the table or does MySQL handle SELECT * in a different way to SELECT first_field, a_field, last_field.
To my understanding, if I had a query that did SELECT first_field, a_field FROM table then it would bring performance benefits if we created an index on first_field, a_field but if we use SELECT * is there even a benefit from creating an index for all fields?
Performing a SELECT * FROM mytable query would have to read all the data from the table. This could, theoretically, be done from an index if you have an index on all the columns, but it would be just faster for the database to read the table itself.
If you have a where clause, having an index on (some of) the columns you have conditions on may dramatically improve the query's performance. It's a gross simplification, but what basically happens is the following:
The appropriate rows are filtered according to the where clause. It's much faster to search for these rows in an index (which is, essentially, a sorted tree) than a table (which is an unordered set of rows).
For the columns that where in the index used in the previous step the values are returned.
For the columns that aren't, the table is accessed (according to a pointer kept in the index).
indexing a mysql table for a column improves performance when there is a need to search or edit a row/record based on that column of that table.
for example, if there is an 'id' column and if it is a primary key; And in that case if you want to search a record using where clause on that 'id' column then you don't need to create index for the 'id' column because primary key column will act as an indexed column.
In another case, if there is an 'pid' column in the table and if it is not a primary key; Then in order to search based on 'pid' column then to improve performance it is better to create an index for the 'pid' column. That will make query fast to search the expected record.

Mysql query stuck in sending data

my query is taking long time to process ans remains in "sending data".
SELECT items.defindex,items.name,items.image_url,price.median as price,user_items.id ,user_items.item_id,user_items.original_id
FROM user_items
INNER JOIN items ON user_items.defindex=items.defindex
LEFT JOIN price ON user_items.defindex=price.defindex && user_items.quality=price.quality
WHERE user_items.user_id=6 && user_items.flag_cannot_trade=0 && price.price>=0 && items.price<=40 && items.banned=0
ORDER by price.median desc
and below is the explain output
Index of all three tables
Ill provide more info if requested
Thanks
OK from the available indexes its clear that you are missing indexes which would be needed when you deal with large data
Your tables are joined with the column defindex and its only indexed in price
So first thing add indexes on other two tables.
alter table items add index defindex_idx(defindex);
alter table user_items add index defindex_idx(defindex);
Now you have where condition and order by , for optimizer to scan less number of rows you need to add more indexes.
alter table user_items add index uid_flag_idx(user_id,flag_cannot_trade);
alter table price add index price_idx(price);
alter table items add index price_idx(price);
alter table items add index banned_idx(banned);
alter table price add index median_idx(median);
Make sure to take a backup of the table before applying the indexes.
And also try price.median in the selection part.
Create indexes for all this fields:
user_items.defindex
items.defindex
user_items.defindex
price.defindex
user_items.quality
price.quality
user_items.user_id
user_items.flag_cannot_trade
price.price
items.price
items.banned
price.median

Two composite indexes or one?

I have two queries that are as follows:
SELECT * FROM table WHERE asset_type=%s AND country=%s AND series=%s
SELECT * FROM table WHERE asset_type=%s AND country=%s AND episode=%s
Should I add one composite index, including all four fields? Or two composite indexes, one for each query?
ALTER TABLE table ADD INDEX (asset_type, country, series)
ALTER TABLE table ADD INDEX (asset_type, country, episode)
-- or --
ALTER TABLE table ADD INDEX (asset_type, country, series, episode)
Why should I choose one over the other?
If you want to maximally optimize both queries, then use two indexes.
If the asset_type and country fields are highly selective -- meaning that they select very few of the rows in the original table -- then the one index will work. Note that for the second query, the first two keys will be used to find a position in the index to start scanning.

Mysql multiple columns index

UPDATE album SET x=1 WHERE store_id=:store_id && type=:type && time<:time
I have a Mysql update query, My question is how can I set up the index for this query
I create index in phpMyadmin, should I select store_id, type, time together and create one index?
if you are searching by store_id and type and time together then yes you can create INDEX for those three.
BUT,
if sometimes you are searching only by store_id then here you should use index only in store_id
if you search by store_id and type then index will be on those two columns.
so it depeneds what are columns you using to search.
here how to use to create what index you want.
ALTER TABLE `album` ADD INDEX `myindex` (`store_id`) --for store_id
ALTER TABLE `album` ADD INDEX `myindex` (`store_id` ,`type`,`time`) --for store_id and type and time
and so on ....
choose which one you want.
When setting up an index, the place to start is the where clause:
WHERE store_id=:store_id && type=:type && time<:time
Start with the equality comparisons. Then you can choose one column for inequality. For this query, the best index would have all three columns:
create index album_storeid_type_time on album(store_id, type, time);

suitable indexes for where group-by plus plus order-by

I have query that uses order-by group-by
select count(*),filed2
from table1 where field1>x group by filed2 order by count(*) desc
what are the best indexes for this query.
sholud I index filed1,field2 seprate or together?
You should create the index with both columns in two different orders
ALTER TABLE table1 ADD INDEX field1_field2_ndx (field1,field2);
ALTER TABLE table1 ADD INDEX field2_field1_ndx (field2,field1);
You should not create individual indexes because making the index with both columns will cause the query to pass through the index only to satisfy the query. It would never need to touch the table.
Even if you made individual indexes, the Query Optimizer would choose the two column index anyway.
Now that you have the two indexes, just trust the Query Optimizer to select the correct index. Based on the query, the EXPLAIN plan would choose the field2_field1_ndx index.