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
Related
I have a table with 500k rows. I have specific table which takes really long time to run every query.
One of the queries is:
SELECT *
FROM player_data
WHERE `user_id` = '61120'
AND `opzak` = 'ja'
ORDER BY opzak_nummer ASC
the opzak_nummer column is a tinyint with a number.
EXPLAIN:
Is there any way to improve this query performance and the general of this query/table?
The table name is player_data and includes about 25 columns, most of them are integers with values of stats.
The index is id AUTO_INCREMENT.
You need to run that query, it will alter table and add index. You can read more details here http://dev.mysql.com/doc/refman/5.7/en/drop-index.html
ALTER TABLE pokemon_speler ADD INDEX index_name (user_id, opzak);
The optimal index for that query is either of these:
INDEX(user_id, opzak, opzak_nummer)
INDEX(opzak, user_id, opzak_nummer)
The first two columns do the filtering; the last avoids a tmp table and sort by consuming the ORDER BY.
Is any combination of columns 'unique' (other than id)? If so, we might be able to make it run even faster.
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.
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.
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.
I need to retrieve columns from two tables and I have used an INNER JOIN. But its consuming lot of time during loading the page. Is there any better and faster way to achieve the same?
Select P.Col1, P.Col2, P.Col3, P.Col4, P.Col5, C.Col1, C.Col2, C.Col3 from Pyalers P inner join Customers C on C.Col1 = P.Col1 where P.Col2 = 5
Thanks in Advance.
Without knowing your DDL, there's no way to say.
But conceptually this is ok, just be sure you have proper indexs sets.
For instance: (is your table name really 'Pyalers'? Assuming 'players')
CREATE INDEX idx_players ON `players` (col1);
CREATE INDEX idx_customers ON `customers` (col1);
use the columns you need for joinning the 2 tables.
http://dev.mysql.com/doc/refman/5.0/en/create-index.html
You're doing it the right way, but if you don't have indexes on your tables on the correct columns, it's not going to be very fast for tables of any size. Do Pyalers.col1 and Customers.col1 both have indexes on them?
Show us how the tables are defined.
Be sure your table has the needed indexes... as a "thumb rule", every field which is used for search (WHERE) or data joins (INNER JOIN, LEFT JOIN, RIGHT JOIN) should be indexed.
Example: If you are creating a table, you can add your indexes at that time (notice that your tables should always have a primary key):
CREATE TABLE myTable (
myId int unsigned not null,
someField varchar(50),
primary key (myId),
index someIdx(someField)
);
If your table already exists, and you want to add indexes, you need to use the ALTER statement:
ALTER TABLE myTable
ADD INDEX someIdx(someField),
ADD PRIMARY KEY (myId);
Rules:
To define an index you most provide a unique name for it, and specify the fields included in the index: INDEX myIndex(field1, field2, ...)
There are different types of indexes: PRIMARY KEY is used for primary keys (that's obvious, huh?); INDEX is an 'ordinary index', just used to speed up search and join operations; UNIQUE INDEX is an index that prevents duplicate values.
Recomendations:
Whenever you can, index all numeric and date fields that are relevant (ids, birth date, etc.). Avoid creating indexes on fields that contain 'double' values.
Don't abuse of indexes, because abuse can create very large index files.
Tips:
If you want to see how your query will be executed, you can use the EXPLAIN statement:
EXPLAIN SELECT a., b. FROM a INNER JOIN b on a.myId = b.otherId
This instruction will show you the execution plan of the query. If in the last column you see 'file sort' or 'using temporary', you may (just may) need aditional indexes (notice that if you use GROUP BY you will almost always get the 'using temporary' message)
Hope this help you