mysqli - Better performance for MySQL query getting last or most recent score of the day for each day -


so, work needed, may have performance issues when used full test of hundreds of questions million tests, etc.

  • each test creates 1 row in "tests" table
  • query should group day of month
  • should grab recent test of day, if minute.

i'm sort of sql newbie, performance tips or schema suggestions on how better appreciated. much.

hopefully, sql fiddle link work: http://sqlfiddle.com/#!9/ef8ef/1

-- last test score of day each day.  create table tests (   id int(10) unsigned not null auto_increment,   user_key char(32) collate utf8mb4_unicode_ci not null,    q1_answer tinyint(3) unsigned not null,   q2_answer tinyint(3) unsigned not null,   date_taken timestamp not null default current_timestamp,   primary key(id) ) engine=innodb default charset=utf8mb4 collate=utf8mb4_unicode_ci;  insert tests values (default, '007userkey', 11, 33, '2016-01-10 16:01:01'), (default, '007userkey', 3, 55, '2016-01-10 19:01:01'), (default, '007userkey', 100, 44, '2017-03-03 12:01:01'), (default, '007userkey', 37, 66, '2017-06-10 15:01:01'), (default, '007userkey', 96, 77, '2017-06-10 20:12:01'), (default, '007userkey', 6, 99, '2017-06-10 20:13:01'), (default, '007userkey', 2, 22, '2017-09-12 00:01:01'); 

-- works, each test question?  -- question #1 select date_format(t1.date_taken, '%y-%m-%d') date,   t1.q1_answer q1score (   select max(date_taken) maxval     tests     user_key = '007userkey'      group date_format(date_taken, '%y-%m-%d') ) t2   inner join tests t1   on t1.date_taken = t2.maxval   order date_format(date_taken, '%y-%m-%d') asc;  -- question #2 select date_format(t1.date_taken, '%y-%m-%d') date,   t1.q2_answer q2score (   select max(date_taken) maxval     tests     user_key = '007userkey'      group date_format(date_taken, '%y-%m-%d') ) t2   inner join tests t1   on t1.date_taken = t2.maxval   order date_format(date_taken, '%y-%m-%d') asc; 

desired result example (as shown in sql fiddle) have recent or last score of each test each day, formatted so:

date         q1score 2016-01-10   3 2017-03-03   100 2017-06-10   6 2017-09-12   2  date         q2score 2016-01-10   55 2017-03-03   44 2017-06-10   99 2017-09-12   22 

fyi... php use make json-based charts each question similar this:

"q1scores":[{"day_taken":"2017-09-04","score":100},{"day_taken":"2017-09-10","score":100}] "q2scores":[{"day_taken":"2017-09-04","score":100},{"day_taken":"2017-09-10","score":100}] 


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -