本节讨论MySQL是如何进行数据查询和读取的。首先看一下调用过程:
JOIN::exec()--> do_select()--> sub_select()--> read_record()实际上是join_read_next()或join_read_first()--> ha_index_next--> index_next()实际上是ha_innobase::index_next()--> general_fetch()--> row_search_mvcc()
ha_innobase是从handler派生出来的,用来做实际查询的。里面的重要结构有:
/** Save CPU time with prebuilt/cached data structures */ row_prebuilt_t* m_prebuilt; /** Thread handle of the user currently using the handler; this is set in external_lock function */ THD* m_user_thd;
这里面最重要的是m_prebuilt结构,其中比较重要的几项:
btr_pcur_t* pcur; /*!< persistent cursor used in selects
and updates */
innodb_session_t*
session; /*!< InnoDB session handler. */
/** The MySQL table object */
TABLE* m_mysql_table;
/** The MySQL handler object. */
ha_innobase* m_mysql_handler;handler是由table调用的,常用的方式是table->file->**,这里的file实际上就是handler。而table在optimizer中被赋值的: TABLE_LIST *tl= select_lex->leaf_tables;,因为用了select_lex,就表明一个THD所有的操作都是共享table的。
m_prebuilt->pcur->btr_cur.page_cur.offsets