我在这个MySQL查询上的索引有什么问题?

我在这个MySQL查询上的索引有什么问题?,mysql,database,Mysql,Database,给出: select xml_record_product.product_id, IfNull(xml_record_product.product_short_description,xml_record_product.product_description) AS BookDescription, xml_record_product.product_image,

给出:

select
                xml_record_product.product_id, 
                IfNull(xml_record_product.product_short_description,xml_record_product.product_description) AS BookDescription, 
                xml_record_product.product_image, 
                xml_record_product.product_publisher_name AS Publisher, 
                xml_record_product.product_title AS BookTitle, 
                xml_record_product.product_form, 
                xml_record_product.product_num_pages, 
                xml_record_product.product_BASICMainSubject, 
                xml_record_product.product_BICMainSubject, 
                xml_record_product.product_audience_code, 
                xml_record_product.product_country_of_publication, 
                xml_record_product.product_publishing_status, 
                xml_record_product.product_publication_date AS BookDate, 
                xml_record_product.product_imprint, 
                xml_record_product.product_active, 
                xml_record_product.product_isFeatured, 
                xml_record_product.product_isNewArival, 
                xml_record_product.product_short_description, 
                xml_record_product.product_description, 
                xml_record_product.product_isbn13 AS ISBN,
                xml_record_subject.subject_heading_text
                FROM xml_record_product 
                inner join xml_record_contributor ON xml_record_product.product_id = xml_record_contributor.product_id
                inner join xml_record_subject on xml_record_contributor.product_id = xml_record_subject.product_id
                inner join xml_record_supplier on xml_record_product.product_id = xml_record_supplier.product_id AND supplier_price > 0
                where contributor_title like '%josh%'
                order by xml_record_product.product_publication_date DESC limit 20;
但是如果我去掉xml\u record\u supplier上的内部连接,我会得到以下结果:

SIMPLE xml_record_supplier index sup_product_id sup_product_id 265  895424 Using where; Using index; Using temporary; Using filesort

SIMPLE xml_record_subject ref product_id_sub product_id_sub 8 mysupplier1.xml_record_supplier.product_id 1 

SIMPLE xml_record_product eq_ref PRIMARY PRIMARY 8 mysupplier1.xml_record_supplier.product_id 

SIMPLE xml_record_contributor ref cont_product_id cont_product_id 8 mysupplier1.xml_record_subject.product_id 1 Using where
我需要知道为什么会发生这种情况,以及如何预防?据我所知,EXPLAIN将显示从内部联接顺序读取的表。这是没有发生的,我已经确保在这些xml\u记录\u贡献者查询中在product\u id上设置了索引。它是product\u id和contributor\u id的混合

xml_record_product (index is product_id,product_publication_date desc)

product_id  bigint(20)  NO  PRI     auto_increment
    product_isbn13  bigint(13)  NO          
    product_form    varchar(255)    NO          
    product_num_pages   int(11) NO          
    product_BASICMainSubject    varchar(255)    NO          
    product_BICMainSubject  varchar(255)    NO          
    product_audience_code   int(11) NO          
    product_country_of_publication  varchar(255)    NO          
    product_publishing_status   int(11) NO          
    product_publication_date    varchar(255)    NO  MUL     
    product_short_description   varchar(350)    NO          
    product_description text    NO          
    product_imprint varchar(255)    NO          
    product_image   varchar(255)    NO          
    product_publisher_name  varchar(255)    NO          
    product_title   varchar(255)    NO          
    product_active  int(11) NO      1   
    cat_id  int(11) NO          
    product_isFeatured  int(11) NO          
    product_isNewArival int(11) NO  
在xml记录供应商上,我在索引中有产品id和供应商价格,但是,我也尝试了许多组合,认为所有组合都是这样

你知道我能试试什么吗?我需要将选择范围限制在价格高于0的供应商,随着内部连接的增加,它很快就会失控

谢谢你的意见

/编辑-以下是对所有表的描述

SIMPLE xml_record_product index PRIMARY pub_date 265  20 

SIMPLE xml_record_subject ref product_id_sub product_id_sub 8 mysupplier1.xml_record_product.product_id 1 

SIMPLE xml_record_contributor ref cont_product_id cont_product_id 8 mysupplier1.xml_record_subject.product_id 1 Using where
xml\u记录\u贡献者索引是产品\u id,贡献者\u id

xml_record_product (index is product_id,product_publication_date desc)

product_id  bigint(20)  NO  PRI     auto_increment
    product_isbn13  bigint(13)  NO          
    product_form    varchar(255)    NO          
    product_num_pages   int(11) NO          
    product_BASICMainSubject    varchar(255)    NO          
    product_BICMainSubject  varchar(255)    NO          
    product_audience_code   int(11) NO          
    product_country_of_publication  varchar(255)    NO          
    product_publishing_status   int(11) NO          
    product_publication_date    varchar(255)    NO  MUL     
    product_short_description   varchar(350)    NO          
    product_description text    NO          
    product_imprint varchar(255)    NO          
    product_image   varchar(255)    NO          
    product_publisher_name  varchar(255)    NO          
    product_title   varchar(255)    NO          
    product_active  int(11) NO      1   
    cat_id  int(11) NO          
    product_isFeatured  int(11) NO          
    product_isNewArival int(11) NO  
xml\记录\供应商索引是产品\标识、供应商\标识、供应商\价格

contributor_id  int(11) NO  PRI     auto_increment
product_id  bigint(20)  NO  MUL     
contributor_title   varchar(255)    NO          
xml_记录_主题索引是主题id、产品id

supplier_id int(11) NO  PRI     auto_increment
product_id  bigint(20)  NO  MUL     
supplier_name   varchar(255)    NO          
supplier_product_availability   int(11) NO          
supplier_price  varchar(255)    NO          
supplier_currency_code  varchar(255)    NO          
supplier_supply_to  varchar(255)    NO          
supplier_price_status   varchar(255)    NO          
Description text    NO          
URL varchar(255)    NO          
Image1  varchar(255)    NO          
Image1_sml  varchar(255)    NO          
Image1Alt   varchar(255)    NO          
编辑

以下是xml\u记录\u供应商的显示索引

subject_id  int(11) NO  PRI     auto_increment
product_id  bigint(20)  NO  MUL     
subject_heading_text    varchar(255)    YES MUL     
ParentID    varchar(20) NO  

你能发布一个描述表以便我们能看到你的索引和表结构吗

我总是让我的连接尽可能简单。将供应商价格>0移动到WHERE条件。我猜是因为这种情况,MySQL试图在xml\u记录中使用索引。因此,它不能在产品发布日期使用索引,这意味着它必须在没有索引的情况下进行排序。如果是这样的话,就没有一个简单的解决方案,因为您无法用不同表中的字段构建复合索引


MySQL的计划者更喜欢减少行数,而不是优化订单数量。根据结果数、表中的行数和基数,取消文件排序可能会更快。尝试在_title_和_date上使用FORCE INDEX composite _INDEX_,以删除文件排序并比较结果。

原始代码的WHERE语句中的supplier_price>0,但将其从那里更改为内部联接和/或返回不会更改解释。我已按要求添加了描述。我确实尝试过强制索引,但它似乎根本无助于删除所有索引并从头开始,MySQL似乎选择了最佳选项,当我在从xml\u记录\u供应商中选择产品\u id时,解释放弃了使用临时/使用文件排序,但由于明显的原因仍然运行缓慢。这些索引是复合索引吗?SHOW INDEXES FROM table将为您提供一个表中的所有索引。我在表上为您添加了SHOW index,该表在内部联接时会出现问题。我不确定它是否是一个综合指数,但我肯定它一定是。