改进/优化PHP,MySQL,Zend中的性能查询

问题描述 投票:0回答:1

我有发票,发票订单,订单,订单_订单。发票和订单表包含大约1百万条记录。 Invoices_items和Orders_items表包含超过2百万条记录。项目表包含2万条记录。现在我想基于我的过滤器生成报告,如客户,项目类别等....请参考查询。

在PHP 5.6上运行。 MySql 5.7和Apache2。

SELECT
  `si_items`.`item_id`
  , SUM(qty) AS `qty`
  , IFNULL(SUM(selling_price * (qty)), 0) AS `salestotal`
  , GROUP_CONCAT(si.id) AS `siso_id` 
  , MAX(si.date_transaction) AS `date_transaction`
FROM
  `invoice_items` AS `si_items`
  LEFT JOIN `invoice` AS `si`
    ON si.id = si_items.parent_id
  LEFT JOIN `items`
    ON si_items.item_id = items.id
WHERE (
    DATE_FORMAT(si.date_transaction, '%Y-%m-%d') BETWEEN '2019-01-01'
    AND '2019-02-15'
  )
  AND (si.approved = 1)
  AND (si.deleted = 0)
  AND (items.deleted = 0)
    GROUP BY `item_id`

     UNION

SELECT
  `so_items`.`item_id`
  , SUM(qty) AS `qty`
  , IFNULL(SUM(selling_price * (qty)), 0) AS `salestotal`
  , GROUP_CONCAT(so.id) AS `soso_id` 
  , MAX(so.date_transaction) AS `date_transaction`
FROM
  `order_items` AS `so_items`
  LEFT JOIN `order` AS `so`
    ON so.id = so_items.parent_id
  LEFT JOIN `items`
    ON so_items.item_id = items.id
WHERE (
    DATE_FORMAT(so.date_transaction, '%Y-%m-%d') BETWEEN '2019-01-01'
    AND '2019-02-15'
  )
  AND (so.approved = 1)
  AND (so.deleted = 0)
  AND (items.deleted = 0)
    GROUP BY `item_id`

当我执行此查询50天。执行此查询花了1分20秒。

INDEXES添加在表格中

发票和订单表

PRIMARY KEY (`id`),
KEY `account_id` (`account_id`),
KEY `approved` (`approved`),
KEY `deleted` (`deleted`),
KEY `finalised` (`finalised`),
KEY `rp_status` (`rp_status`),
KEY `sales_types_id` (`sales_types_id`),
KEY `account_type_id` (`account_type_id`),
KEY `company_id` (`company_id`),
KEY `date_transaction` (`date_transaction`)

Invoices_items和Order_items

PRIMARY KEY (`id`),
KEY `deleted` (`deleted`),
KEY `item_id` (`item_id`),
KEY `parent_id` (`parent_id`),
KEY `vat_id` (`vat_id`),
KEY `qty` (`qty`),

解释查询

Explain Query

我需要提高此查询的性能。你能指导我怎么办吗?

显示创建表

CREATE TABLE `invoice` (
  `id` char(36) NOT NULL,
  `reference` varchar(25) DEFAULT NULL,
  `company_id` char(36) DEFAULT NULL,
  `branch_id` char(36) DEFAULT NULL,
  `account_id` char(36) DEFAULT NULL,
  `contact_id` char(36) DEFAULT NULL,
  `transaction_type` varchar(10) DEFAULT NULL,
  `sales_types_id` int(11) DEFAULT '0',
  `quote_validity` int(11) DEFAULT '0',
  `delivery_method_id` int(11) DEFAULT '0',
  `sales_representative_id` int(11) DEFAULT '0',
  `account_type_id` char(36) DEFAULT NULL,
  `vat_exempted` tinyint(1) DEFAULT '0',
  `description` text,
  `finalised` tinyint(1) DEFAULT '0' COMMENT 'Not Yet finalised - status=1; Need Approval - status = 2; Approved - status = 3',
  `approved` tinyint(1) DEFAULT '0',
  `approved_user_id` int(11) DEFAULT '0',
  `default_sales_location_id` char(36) DEFAULT NULL COMMENT '0-Yes; 1-No',
  `generate_do` tinyint(1) DEFAULT '1',
  `generate_dn` tinyint(4) DEFAULT '1',
  `do_status` tinyint(1) DEFAULT '0',
  `cn_status` tinyint(1) DEFAULT '0',
  `rp_status` tinyint(1) DEFAULT '0',
  `dm_status` tinyint(1) DEFAULT '0',
  `currency_id` char(36),
  `exchange_rate_id` tinyint(1) DEFAULT '0',
  `exchange_rate` double DEFAULT '1',
  `date_transaction` datetime DEFAULT NULL,
  `date_created` datetime DEFAULT NULL,
  `date_modified` datetime DEFAULT NULL,
  `created_user_id` int(11) DEFAULT '0',
  `modified_user_id` int(11) DEFAULT '0',
  `deleted` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `account_id` (`account_id`),
  KEY `approved` (`approved`),
  KEY `branch_id` (`branch_id`),
  KEY `cn_status` (`cn_status`),
  KEY `created_user_id` (`created_user_id`),
  KEY `date_created` (`date_created`),
  KEY `deleted` (`deleted`),
  KEY `do_status` (`do_status`),
  KEY `finalised` (`finalised`),
  KEY `reference` (`reference`),
  KEY `rp_status` (`rp_status`),
  KEY `sales_types_id` (`sales_types_id`),
  KEY `account_type_id` (`account_type_id`),
  KEY `company_id` (`company_id`),
  KEY `date_transaction` (`date_transaction`),
  KEY `default_sales_location_id` (`default_sales_location_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `invoice_items` (
  `id` char(36) NOT NULL,
  `parent_id` char(36) DEFAULT NULL,
  `item_id` char(36) DEFAULT NULL,
  `qty` double DEFAULT '0',
  `cost_price` double DEFAULT '0',
  `list_price` double DEFAULT '0',
  `selling_price` double DEFAULT '0',
  `unit_price` double DEFAULT '0',
  `vat` double DEFAULT '0',
  `amount` double DEFAULT '0',
  `special_discount` double DEFAULT '0',
  `price_change_status` tinyint(1) DEFAULT '0',
  `remarks` text,
  `vat_id` int(11) DEFAULT '1',
  `stock_category_id` tinyint(2) DEFAULT '0' COMMENT '1: Stockable 2: Service',
  `is_giftitem` tinyint(1) DEFAULT '0' COMMENT '1: Gift Item 0: NO Gift',
  `item_type_status` tinyint(1) DEFAULT '0',
  `date_created` datetime DEFAULT NULL,
  `date_modified` datetime DEFAULT NULL,
  `created_user_id` int(11) DEFAULT '0',
  `modified_user_id` int(11) DEFAULT '0',
  `deleted` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `deleted` (`deleted`),
  KEY `item_id` (`item_id`),
  KEY `parent_id` (`parent_id`),
  KEY `stock_category_id` (`stock_category_id`),
  KEY `item_type_status` (`item_type_status`),
  KEY `vat_id` (`vat_id`),
  KEY `amount` (`amount`),
  KEY `qty` (`qty`),
  KEY `unit_price` (`unit_price`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
php mysql zend-framework database-performance
1个回答
0
投票

当你的意思是LEFT JOIN时,不要使用JOIN。特别是,加入si

WHERE (
    DATE_FORMAT(si.date_transaction, '%Y-%m-%d') BETWEEN '2019-01-01'
    AND '2019-02-15'
  )

-->

WHERE si.date_transaction >= '2019-01-01'
  AND si.date_transaction  < '2019-01-15'

以便索引(见下文)可以使用该列

WHERE  si.date_transaction ...
  AND (si.approved = 1)
  AND (si.deleted = 0)

添加复合索引:

INDEX(deleted, approved,   -- in either order
      date_transaction)    -- last

so进行类似的更改。然后让我们听听表演的表现,看看EXPLAIN改变了什么。

的UUID

谨防UUID,它们体积庞大且速度慢。如果无法缓存整个表,它们会特别慢。

我怀疑你有uuids因为我看到CHAR(36)

通过使用CHARACTER SET utf8,这意味着正在使用108个字节! UUID可以打包成16字节的BINARY(16)。这将有助于空间(以及速度)。

但UUID的真正问题在于随机性。一旦表变大,系统就会受I / O限制,因为“下一个”UUID不太可能被缓存。

考虑切换到AUTO_INCREMENT ID。对于单服务器系统,这是首选。如果您需要从多个位置生成ID,您可能仍需要UUID。

关于UUID的More

© www.soinside.com 2019 - 2024. All rights reserved.