MCPcopy Create free account
hub / github.com/Meituan-Dianping/SQLAdvisor / mysql_sql_parse_order

Function mysql_sql_parse_order

sqladvisor/main.cc:812–857  ·  view source on GitHub ↗

同理,同时判断order by 表是否相同,还要判断升降序是否一致

Source from the content-addressed store, hash-verified

810
811//同理,同时判断order by 表是否相同,还要判断升降序是否一致
812TABLE_LIST* mysql_sql_parse_order() {
813 TABLE_LIST * table = NULL;
814 ORDER::enum_order dire;
815 bool is_only_one_table = true;
816 SQL_I_List<ORDER> order_list = current_lex->order_list;
817 int order_elemnts = order_list.elements;
818 int order_elment_i = 0;
819 if (order_elemnts > 0) {
820 if (options.verbose)
821 sql_print_information("第%d步:开始解析order by 条件 \n", STEP_CNT++);
822 ORDER *order_field = order_list.first;
823 while (order_field != NULL) {
824 order_elment_i++;
825 if (order_field->item_ptr->type() == Item::FIELD_ITEM) {
826 TABLE_LIST * tbname2 = find_table((Item_field *) (order_field->item_ptr));
827 if (tbname2 == NULL) {
828 is_only_one_table = false;
829 break;
830 }
831 if (isPrimary(((Item_field *) (order_field->item_ptr))->field_name,tbname2->table_name)) {
832 if (order_elment_i == order_elemnts) //当order by c1,c2...中包含主键时,只有当主键在最后位置则忽略,否则无法使用order by索引
833 break;
834 else if (options.verbose)
835 sql_print_information("第%d步:字段%s是主键同时不在order by列表末尾,放弃处理order by条件 \n",STEP_CNT++,((Item_field *) (order_field->item_ptr))->field_name);
836 return NULL;
837 }
838 ORDER::enum_order dire2 = order_field->direction;
839 if (table == NULL) {
840 table = tbname2;
841 dire = dire2;
842 } else if (table->table_name != tbname2->table_name || dire != dire2) {
843 is_only_one_table = false;
844 break;
845 }
846 }
847 order_field = order_field->next;
848 }
849
850 if (is_only_one_table && table) {
851 if (TABLE_ELEMENT == 1 || table->is_table_driverd) {
852 return table;
853 }
854 }
855 }
856 return NULL;
857}
858
859void mysql_sql_parse_group_order_add(TABLE_LIST *table,
860 SQL_I_List<ORDER> order_list) {

Callers 1

mysql_sql_parse_adminFunction · 0.85

Calls 4

sql_print_informationFunction · 0.85
find_tableFunction · 0.85
isPrimaryFunction · 0.85
typeMethod · 0.45

Tested by

no test coverage detected