MySQL source code reading 2 - basic flow

Introduction

This article follows http://d.hatena.ne.jp/takahirox/20140331/1396222792.

I'll confirm the basic query processing flow in this article.

Basic flow

The basic processing is written in the internal manual.


And, the internal manual shows us the server sample code with insert.


The server processes a query as followings after it receives the query from a client.

  1. parse a SQL
  2. optimizes(optimizes a query and decide how to handle it)
    1. this is what I wanna secondly see
  3. handling a query
    1. this is what I wanna first see
  4. return results

Select

What I first wanna see is select. This is the backtrace at the point of my_read() when I executed select. my_read() calls libc read() to read table data from disk, it's the lowest function.

#0  my_read (Filedes=40, Buffer=0xa00117f8 "", Count=90, MyFlags=32)
    at /home/takahiro/mysql-server/mysql-5.6/mysys/my_read.c:50
#1  0x08674b65 in inline_mysql_file_read (
    src_file=0x8bbf580 "/home/takahiro/mysql-server/mysql-5.6/mysys/mf_iocache.c
    at /home/takahiro/mysql-server/mysql-5.6/include/mysql/psi/mysql_file.h:1100
#2  0x08675e46 in _my_b_read (info=0xa000fad0, Buffer=0xa000f250 "\377", 
    Count=9) at /home/takahiro/mysql-server/mysql-5.6/mysys/mf_iocache.c:562
#3  0x08996191 in _mi_read_rnd_static_record (info=0xa000f968, 
    buf=0xa000f250 "\377", filepos=0, skip_deleted_blocks=1 '\001')
    at /home/takahiro/mysql-server/mysql-5.6/storage/myisam/mi_statrec.c:279
#4  0x0898f90a in mi_scan (info=0xa000f968, buf=0xa000f250 "\377")
    at /home/takahiro/mysql-server/mysql-5.6/storage/myisam/mi_scan.c:45
#5  0x089560a3 in ha_myisam::rnd_next (this=0xa000f0c8, buf=0xa000f250 "\377")
    at /home/takahiro/mysql-server/mysql-5.6/storage/myisam/ha_myisam.cc:1758
#6  0x082399ed in handler::ha_rnd_next (this=0xa000f0c8, buf=0xa000f250 "\377")
    at /home/takahiro/mysql-server/mysql-5.6/sql/handler.cc:2638
#7  0x085708cb in rr_sequential (info=0xa0005a70)
    at /home/takahiro/mysql-server/mysql-5.6/sql/records.cc:478
#8  0x0839e09a in join_init_read_record (tab=0xa0005a24)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_executor.cc:2392
#9  0x0839b8fc in sub_select (join=0xa0005028, join_tab=0xa0005a24, 
    end_of_records=false)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_executor.cc:1253
#10 0x0839b324 in do_select (join=0xa0005028)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_executor.cc:930
#11 0x0839934e in JOIN::exec (this=0xa0005028)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_executor.cc:191
#12 0x083f8959 in mysql_execute_select (thd=0x95a7368, select_lex=0x95a9040, 
    free_join=true)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_select.cc:1100
#13 0x083f8c88 in mysql_select (thd=0x95a7368, tables=0xa0004bc0, wild_num=1, 
    fields=..., conds=0x0, order=0x95a9134, group=0x95a90d0, having=0x0, 
    select_options=2147748608, result=0xa0005010, unit=0x95a8bdc, 
    select_lex=0x95a9040)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_select.cc:1221
#14 0x083f6d86 in handle_select (thd=0x95a7368, result=0xa0005010, 
    setup_tables_done_option=0)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_select.cc:110
#15 0x083d1fda in execute_sqlcom_select (thd=0x95a7368, all_tables=0xa0004bc0)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_parse.cc:5094
#16 0x083ca2d0 in mysql_execute_command (thd=0x95a7368)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_parse.cc:2642
#17 0x083d4595 in mysql_parse (thd=0x95a7368, 
    rawbuf=0xa0004a78 "select * from a", length=15, parser_state=0xa4dcda18)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_parse.cc:6235
#18 0x083c72f0 in dispatch_command (command=COM_QUERY, thd=0x95a7368, 
    packet=0x967e781 "", packet_length=15)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_parse.cc:1334
#19 0x083c64b7 in do_command (thd=0x95a7368)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_parse.cc:1036
#20 0x0838e99b in do_handle_one_connection (thd_arg=0x95a7368)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_connect.cc:982
#21 0x0838e4cf in handle_one_connection (arg=0x95a7368)
    at /home/takahiro/mysql-server/mysql-5.6/sql/sql_connect.cc:898
#22 0x089ea076 in pfs_spawn_thread (arg=0x95bd540)
    at /home/takahiro/mysql-server/mysql-5.6/storage/perfschema/pfs.cc:1858
#23 0xb7f74d4c in start_thread () from /lib/i386-linux-gnu/libpthread.so.0
#24 0xb7d83bae in clone () from /lib/i386-linux-gnu/libc.so.6

From this back trace and the internal manual, mysql_select() or handle_select() could be the basement function for handling select.

Conclusion

I'll read the code for select next article.