简体   繁体   中英

Mysql query using index using filesort using temporary

I have the following two tables in my database:

Table 1: images

  • columns: jeid [and others]
  • Primary key: jeid
  • Number of rows: 1.6 million

Table 2: media_names

  • columns: jeid, media type, first_name, last_name
  • primary key: jeid, media_type
  • Index: first_name, last_name
  • Number of rows: 1.6 million

I'm running the following query:

SELECT media.jeid FROM image AS media
LEFT JOIN file_girls_names AS name ON name.jeid = media.jeid AND name.media_type = "image"
ORDER BY name.first_name, name.last_name
LIMIT 0, 50

When I do an explain on this query, the extra column tells me:

  • Using index;
  • Using temporary;
  • Using filesort

I'm not sure that 'using index' is bad, but I'm pretty sure that 'using temporary' and 'using filesort' are bad. The problem is, I have no idea what to do to resolve these. I've been reading all sorts of this and that, but I can't seem to find a definitive solution. Can anyone give me some assistance?

Thank you.

There's more than one way to write a query, You'll have to do some testing, anyway here's my go on this.

SELECT `names`.`jeid`
FROM `file_girls_names` AS `names`
INNER JOIN `image` AS `media` ON `names`.`jeid` = `media`.`jeid`
WHERE `names`.`media_type` = 'image'
ORDER BY `names`.`first_name`, `names`.`last_name`
LIMIT 0, 50

NOTE:: This assumes you have matching columns in both tables (unless you specifically want a left join then this should be OK).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM