Bug #85900 copying to tmp table
Submitted: 11 Apr 2017 10:58 Modified: 11 Apr 2017 11:56
Reporter: Majid Mardanov Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:5.5.41-37.0-55 OS:CentOS
Assigned to: CPU Architecture:Any
Tags: copying to tmp table

[11 Apr 2017 10:58] Majid Mardanov
Description:
Hi,
I have a table thats show max points and user id for specific year .Number of rows about 4 M.
CREATE TABLE `cms_persons_diary_2016` (
	`survey_id` INT(11) NOT NULL DEFAULT '0',
	`p_id` INT(11) NOT NULL DEFAULT '0',
	`date` DATE NOT NULL,
	`day_kavicent` DECIMAL(10,4) NULL DEFAULT NULL,
	`post_date` DATETIME NULL DEFAULT NULL,
	`month` VARCHAR(10) NOT NULL,
	`weekyear` INT(11) NULL DEFAULT NULL,
	`year` INT(11) NULL DEFAULT NULL,
	INDEX `year` (`year`, `p_id`, `day_kavicent`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
when I run query I get a result I want but it runs about 3 sec
select  sql_big_result q.p,max(q.w) as p from (SELECT  distinct p_id p ,sum(`day_kavicent`) as w 
FROM cms_persons_diary_2016  where year=2016 group by p_id 
order by w desc)as q

Profile shows only copying to tmp table takes that 3 seconds
Temp table  is optimized.I tried to do anything bu can not decrease it more than 3 sec.May be it is  a bug of 5.5 version ? and  it is impossible to do more
tmp_table_size=64M
max_heap_table_size=256M

explain 
1	PRIMARY	<derived2>	ALL	\N	\N	\N	\N	46914	
2	DERIVED	cms_persons_diary_2016	ref	year	year	5		1904117	Using where; Using index; Using temporary; Using filesort

How to repeat:
CREATE TABLE `cms_persons_diary_2016` (
	`survey_id` INT(11) NOT NULL DEFAULT '0',
	`p_id` INT(11) NOT NULL DEFAULT '0',
	`date` DATE NOT NULL,
	`day_kavicent` DECIMAL(10,4) NULL DEFAULT NULL,
	`post_date` DATETIME NULL DEFAULT NULL,
	`month` VARCHAR(10) NOT NULL,
	`weekyear` INT(11) NULL DEFAULT NULL,
	`year` INT(11) NULL DEFAULT NULL,
	INDEX `year` (`year`, `p_id`, `day_kavicent`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
select  sql_big_result q.p,max(q.w) as p from (SELECT  distinct p_id p ,sum(`day_kavicent`) as w 
FROM cms_persons_diary_2016  where year=2016 group by p_id 
order by w desc)as q

tmp_table_size=64M
max_heap_table_size=256M
[11 Apr 2017 11:56] MySQL Verification Team
We're sorry, but the bug system is not the appropriate forum for asking help on using MySQL products. Your problem is not the result of a bug.

Support on using our products is available both free in our forums at http://forums.mysql.com/ and for a reasonable fee direct from our skilled support engineers at http://www.mysql.com/support/

Thank you for your interest in MySQL.