简体   繁体   English

简单查询的MySQL性能问题

[英]MySQL performance issue with simple query

I'm trying to run a more or less simple query on a MySQL table with about 100000 entries (Size: 2319.58MB; avg: 24KB pre row). 我正在尝试在具有约100000个条目的MySQL表上运行一个或多或少的简单查询(大小:2319.58MB; avg:前行24KB)。

This is the query: 这是查询:

SELECT 
  n0_.id AS id0, 
  n0_.sentTime AS sentTime1, 
  n0_.deliveredTime AS deliveredTime2, 
  n0_.queuedTime AS queuedTime3, 
  n0_.message AS message4, 
  n0_.failReason AS failReason5, 
  n0_.targetNumber AS targetNumber6, 
  n0_.sid AS sid7, 
  n0_.targetNumber AS targetNumber8, 
  n0_.sid AS sid9, 
  n0_.subject AS subject10, 
  n0_.targetAddress AS targetAddress11, 
  n0_.priority AS priority12, 
  n0_.targetUrl AS targetUrl13, 
  n0_.discr AS discr14, 
  n0_.notification_state_id AS notification_state_id15, 
  n0_.user_id AS user_id16 
FROM 
  notifications n0_ 
  INNER JOIN notification_states n1_ ON n0_.notification_state_id = n1_.id 
WHERE 
  n0_.discr IN (
    'twilioCallNotification', 'twilioSMSNotification', 
    'emailNotification', 'httpNotification'
  ) 
ORDER BY 
  n0_.id desc 
LIMIT 
  25 OFFSET 0

The query between 100 and 200sec. 100到200秒之间的查询。

This is the create statement of the table: 这是表的create语句:

CREATE TABLE notifications (
    id INT AUTO_INCREMENT NOT NULL,
    notification_state_id INT DEFAULT NULL,
    user_id INT DEFAULT NULL,
    sentTime DATETIME DEFAULT NULL,
    deliveredTime DATETIME DEFAULT NULL,
    queuedTime DATETIME NOT NULL,
    message LONGTEXT NOT NULL,
    failReason LONGTEXT DEFAULT NULL,
    discr VARCHAR(255) NOT NULL,
    targetNumber VARCHAR(1024) DEFAULT NULL,
    sid VARCHAR(34) DEFAULT NULL,
    subject VARCHAR(1024) DEFAULT NULL,
    targetAddress VARCHAR(1024) DEFAULT NULL,
    priority INT DEFAULT NULL,
    targetUrl VARCHAR(1024) DEFAULT NULL,
    INDEX IDX_6000B0D350C80464 (notification_state_id),
    INDEX IDX_6000B0D3A76ED395 (user_id),
    PRIMARY KEY (id)
)  DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB;

ALTER TABLE notifications ADD CONSTRAINT FK_6000B0D350C80464 FOREIGN KEY (notification_state_id) REFERENCES notification_states (id) ON DELETE CASCADE;
ALTER TABLE notifications ADD CONSTRAINT FK_6000B0D3A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;

These queries are created by Doctrine2 (ORM). 这些查询是由Doctrine2(ORM)创建的。 We are using MySQL 5.5.31 on a virtual machine (1GB ram, single core) running Debian 6.0.7. 我们在运行Debian 6.0.7的虚拟机(1GB内存,单核)上使用MySQL 5.5.31。 Why is the performance of the query so bad? 为什么查询性能这么差? Is it a database design fault or is the vbox just to small to handle this amount of data? 是数据库设计错误还是vbox太小而无法处理此数据量?

在discr列上添加索引:

ALTER TABLE `notifications` ADD INDEX `idx_discr` (`discr` ASC) ;

You're filtering by notifications.discr but don't appear to have that indexed. 您正在按notifications.discr进行过滤,但似乎没有对该索引进行索引。 Have you tried adding an index on it? 您是否尝试过在其上添加索引?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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