The Path of
the Query

Sergei Golubchik
MariaDB Corporation

whoami

Big Picture

Caches: tables, threads, privileges, etc Query Cache Client-Server Protocol Parser Prepared Statements Optimizer Range Optimizer handler interface Memory CSV Archive FederatedX CONNECT TokuDB MyISAM Aria Column Store InnoDB Spider RocksDB CREATE ALTER INSERT UPDATE DELETE SHOW SELECT join group by order by  

Moment 1: Arrival

 

Protocol

Read the packet

1202 bool do_command(THD *thd)
1203 {
1267   packet_length= my_net_read_packet(net, 1);
1292   if (packet_length == packet_error)
1330   {
1331     return_value= TRUE;
1332     goto out;
1333   }
1339 
1340   packet= (char*) net->read_pos;
1357 
1359   command= fetch_command(thd, packet);
1391   return_value= dispatch_command(command, thd,
1392         packet+1, packet_length-1, FALSE, FALSE);
1426 out:
1431   return return_value;
1432 }

Command dispatcher

1685   switch (command) {
1701   case COM_REGISTER_SLAVE:
1717   case COM_CHANGE_USER:
1782   case COM_STMT_BULK_EXECUTE:
1787   case COM_STMT_EXECUTE:
1792   case COM_STMT_FETCH:
1797   case COM_STMT_SEND_LONG_DATA:
1802   case COM_STMT_PREPARE:
1807   case COM_STMT_CLOSE:
1817   case COM_QUERY:
2063   case COM_QUIT:
2074   case COM_BINLOG_DUMP:
2106   case COM_REFRESH:
2159   case COM_SHUTDOWN:
2230   case COM_PING:
2272   case COM_DEBUG:
2368   }

mysql_parse

7991 void mysql_parse(THD *thd, char *rawbuf, uint length,
7995 {
8016   lex_start(thd);
8017   thd->reset_for_next_command();
8025   if (query_cache_send_result_to_client(thd, rawbuf, length) <= 0)
8026   {
8029     bool err= parse_sql(thd, parser_state, NULL, true);
8031     if (!err)
8076       error= mysql_execute_command(thd);
8081     else
8091       query_cache_abort(thd, &thd->query_cache_tls);
8098     thd->end_statement();
8099     thd->cleanup_after_query();
8101   }
8102   else
8110     thd->update_stats();
8119 }

Query Cache

Moment 2: Parsing

 

Parser

Item


value= expr->val_int();  // or val_str(), val_real(), …

Preparing for execution

Moment 3: Optimizing

 

Query transformations

More query transformations

Range optimizer

Range optimizer: example

CREATE TABLE ( ... INDEX (a,b) ... );
SELECT ... WHERE a IN (1,2) AND b IN (3,4) OR
                 a=7 AND b BETWEEN 3 AND 8 OR
                 a > 9 AND b < 5;
SEL_TREE: SEL_ARG: WHERE a = 1 a = 2 a = 7 a > 9 b = 3 b = 4 b = 3 b = 4 3 ≤ b ≤ 8  

Execution plan

Statistics used

Moment 3: Executing

 

Join

Sending data

Temporary tables

Filesort

SELECT a,b,c FROM t1 ORDER BY a LIMIT 10

SE API

Questions?