简体   繁体   English

MySQL 在一个非常简单的查询上非常慢

[英]MySQL extremely slow on a very simple query

I'm getting very slow response running a very simple query in a small table (115k records)... It takes about 8sec to respond, and I can't figure out why it's taking that long.在一个小表(115k 条记录)中运行一个非常简单的查询时,我的响应速度非常慢......响应大约需要 8 秒,我不明白为什么要花这么长时间。 Any advice would be awesome任何建议都会很棒

Table:桌子:

CREATE TABLE `financeiro_fluxo` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `branch` int(10) unsigned NOT NULL,
  `abertura` int(10) DEFAULT NULL,
  `origem` int(10) unsigned DEFAULT NULL,
  `status_pagamento` tinyint(3) unsigned DEFAULT NULL,
  `conta` int(10) unsigned NOT NULL,
  `tipo_lancamento` tinyint(3) unsigned NOT NULL,
  `categoria` int(10) unsigned NOT NULL,
  `tipo_entidade` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `entidade` int(10) unsigned DEFAULT NULL,
  `entidade_input` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `tipo_pagamento` tinyint(3) unsigned NOT NULL,
  `parcela` smallint(5) unsigned NOT NULL,
  `parcelas` smallint(5) unsigned NOT NULL,
  `valor` decimal(12,2) NOT NULL,
  `valor_taxa` decimal(12,2) DEFAULT NULL,
  `valor_troco` decimal(12,2) DEFAULT NULL,
  `confirmado` tinyint(3) unsigned DEFAULT NULL,
  `data_confirmacao` datetime DEFAULT NULL,
  `vencimento` date NOT NULL,
  `info` varchar(510) COLLATE utf8_unicode_ci DEFAULT NULL,
  `bandeira` int(10) unsigned DEFAULT NULL,
  `user_add` int(10) unsigned NOT NULL,
  `user_last` int(10) unsigned NOT NULL,
  `param_ref` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
  `param` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `file` int(10) unsigned DEFAULT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
  `status` smallint(6) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=116749 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Query:询问:

SELECT * from financeiro_fluxo

Explain:解释:

id  select_type     table               type    key   key_len   rows   
1   SIMPLE          financeiro_fluxo    ALL                     116244  

The same query running on localhost with the same table, returns in less than a sec...在具有相同表的本地主机上运行的相同查询在不到一秒内返回...

Profile:轮廓:

简介:

Seems you are doing a full table scan because your query does not include any limiting conditions (for example WHERE clause or LIMIT).似乎您正在进行全表扫描,因为您的查询不包含任何限制条件(例如 WHERE 子句或 LIMIT)。 To let the query preform better use indexed columns with some kind of criteria.为了让查询更好地使用具有某种标准的索引列。 What happens if you add WHERE id IS NOT NULL如果添加WHERE id IS NOT NULL会发生什么

I assume you need all the records, if not limit the result set by added conditions in a more specific WHERE clause (on a indexed column) or a LIMIT clause.我假设您需要所有记录,如果不通过更具体的 WHERE 子句(在索引列上)或 LIMIT 子句中添加的条件来限制结果集。

Will the "reports" aggregate data? “报告”会汇总数据吗? Of so, you could speed up the 8 second (remote) query by doing more work in the server, thereby shipping less data across the wire.因此,您可以通过在服务器上做更多的工作来加快 8 秒(远程)查询的速度,从而减少网络传输的数据。

That is, think about whether AVG(..), COUNT(*), SUM(..), MAX(..) , etc can be done in the SELECT .也就是想想AVG(..), COUNT(*), SUM(..), MAX(..)等是否可以在SELECT中完成。

Taking that another step... Build and maintain a "Summary table" that has subtotals (etc).再迈出一步……建立并维护一个包含小计(等)的“汇总表”。 Then, reading (or scanning) the summary table and summing up the subtotals, etc, will be even faster, both in the Server and across the wire.然后,读取(或扫描)汇总表并汇总小计等,在服务器中和通过网络都会更快。

(And I agree with the need to avoid * , and that the 8 seconds is probably due to.network delay (and "bandwidth"). Where is the server geographically? How long does SELECT 1; take?) (而且我同意需要避免* ,并且 8 秒可能是由于网络延迟(和“带宽”)。服务器的地理位置在哪里? SELECT 1;需要多长时间?)

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

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