0

我在 MySQL 上有性能问题。数据库包含大约 400 000 条记录,站点在 Drupal 上运行,所有列都有索引。不幸的是,有些广告没有所有数据,所以我需要使用 LEFT JOIN 而不是 INNER JOIN。

看法:

CREATE VIEW cases_view AS SELECT 
    n.nid AS nid,
        n.created AS created,
        n.type AS type,
        n.status AS status,
        i.field_adpersonid_value AS field_adpersonid_value,
        f.field_adpersonname_value AS field_adpersonname_value,
        l.field_adpersonfamilyname_value AS field_adpersonfamilyname_value,
        b.field_adpersondateofborn_value AS field_adpersondateofborn_value,
        d.field_adpersondateofdied_value AS field_adpersondateofdied_value,
        p.field_adnewspapergroupname_value AS field_adnewspapergroupname,
        c.field_sbfcompanyname_value AS field_sbfcompanyname_value
FROM
    node n
LEFT JOIN field_data_field_adpersonname f ON (f.entity_id = n.nid)
JOIN field_data_field_adpersonid i ON (i.entity_id = n.nid)
LEFT JOIN field_data_field_adpersonfamilyname l ON (l.entity_id = n.nid)
LEFT JOIN field_data_field_adpersondateofborn b ON (b.entity_id = n.nid)
LEFT JOIN field_data_field_adpersondateofdied d ON (d.entity_id = n.nid)
LEFT JOIN field_data_field_adnewspapergroupname p ON (p.entity_id = n.nid)
LEFT JOIN field_data_field_sbfcompanyname c ON (c.entity_id = n.nid)
WHERE
   n.type = 'advertisement' AND n.status = 1 AND  `p`.`field_adnewspapergroupname_value` <> 'Times Newspaper'

然后我用这个查询在这个视图(php)中搜索数据:

SELECT DISTINCT v.field_adpersonname_value AS field_adpersonname_value, v.field_adpersonid_value AS field_adpersonid_value, v.field_adpersonfamilyname_value AS field_adpersonfamilyname_value, v.field_adpersondateofborn_value AS field_adpersondateofborn_value, v.field_adpersondateofdied_value AS field_adpersondateofdied_value, v.field_sbfcompanyname_value AS field_sbfcompanyname_value
FROM 
cases_view v
WHERE  (v.field_adpersonname_value LIKE 'Sample name%' ESCAPE '\\') AND (v.field_adpersonfamilyname_value LIKE '%' ESCAPE '\\') AND (v.field_adpersondateofborn_value LIKE '%' ESCAPE '\\') AND (v.field_adpersondateofdied_value LIKE '%' ESCAPE '\\') 
ORDER BY v.field_adpersonfamilyname_value ASC, v.field_adpersondateofdied_value DESC
LIMIT 20 OFFSET 0

我的数据库配置:

max_connections = 300
connect_timeout = 100
max_user_connections = 300
key_buffer = 256M
query_cache_size = 96MB
query_cache_limit = 4MB
table_cache = 1800
table_definition_cache = 1800
sort_buffer_size = 32M
myisam_sort_buffer_size = 32M
tmp_table_size = 64MB
join_buffer_size = 4M
wait_timeout = 60
innodb_buffer_pool_size = 2G
innodb_log_file_size = 1G
max_heap_table_size = 128M
tmp_table_size = 128M
max_allowed_packet = 32M
thread_cache_size = 128

在我启动 mysql 服务器站点并且搜索速度很快但几个小时后“复制到 tmp 表”开始复制越来越多的数据服务器变得缓慢并最终崩溃。系统有 8GB 的​​ RAM 和它的双核。

请帮忙!

4

1 回答 1

0

我认为在这种特殊情况下,首先查找要选择的行的 ID,然后添加其他表可能会更快。玩一下。

select <fields>
from node n
<joins>
where nid in 
  ( select nid 
    from node 
    where n.status = 1 
      and n.type = 'advertisement
  )

另一种解决方案,特别是如果您只需要另一个表中的一个字段是使用子选择,这听起来很慢,但有时比连接快:

select n.fields,
  (select field_adpersonid_value from field_data_field_adpersonid i ON i.entity_id = n.nid) as field_adpersonid_value
from node n
where .. etc

您也可以尝试解释一下,看看哪里慢。

于 2013-10-10T13:03:19.107 回答