MySQL相同語句在不同實例上執行結果不同的問題記錄

開發有一個sql,在a實例上執行正常執行,b實例上執行報錯。
報錯信息
ERROR 1093 (HY000): You can’t specify target table ‘ver’ for update in FROM clause

對比下2個實例的配置

Variable                  b               a
========================= ========================= =========================
back_log                  250                       850
basedir                   /usr/local/mysql          /usr/local/mysql-5.7.11
character_set_client      utf8mb4                   utf8
character_set_connection  utf8mb4                   utf8
character_set_database    utf8mb4                   utf8
character_set_results     utf8mb4                   utf8
character_set_server      utf8mb4                   utf8
character_sets_dir        /usr/local/mysql/share... /usr/local/mysql-5.7.1...
collation_connection      utf8mb4_unicode_ci        utf8_general_ci
collation_database        utf8mb4_unicode_ci        utf8_general_ci
collation_server          utf8mb4_unicode_ci        utf8_general_ci
datadir                   /home/mysql/data/         /home/mysql_3306/
default_password_lifetime 360                       0
expire_logs_days          7                         0
general_log_file          /home/mysql/data/db-10... /home/mysql_3306/10923...
have_openssl              YES                       DISABLED
have_ssl                  YES                       DISABLED
have_symlink              YES                       DISABLED
host_cache_size           653                       803
hostname                  db-10921102               10923183-eletestserver
innodb_buffer_pool_ins... 8                         1
innodb_buffer_pool_size   4294967296                134217728
innodb_flush_method       O_DIRECT                  
innodb_log_file_size      1073741824                50331648
innodb_page_cleaners      4                         1
innodb_use_native_aio     OFF                       ON
innodb_version            5.7.10                    5.7.11
lc_messages_dir           /usr/local/mysql/share/   /usr/local/mysql-5.7.1...
log_bin_basename          /home/mysql/data/binlog   /home/mysql_3306/bin
log_bin_index             /home/mysql/data/binlo... /home/mysql_3306/bin.i...
log_bin_trust_function... ON                        OFF
log_error                 /home/mysql/data/10921... /home/mysql_3306/mysql...
max_allowed_packet        629145600                 4194304
max_connections           1000                      4000
performance_schema_max... 192                       193
pid_file                  /home/mysql/data/10921... /home/mysql_3306/mysql...
plugin_dir                /usr/local/mysql/lib/p... /usr/local/mysql-5.7.1...
server_uuid               7fc12caf-88ac-11e8-873... 260ba4db-bd00-11e9-bd8...
slow_query_log_file       /home/mysql/data/db-10... /home/mysql_3306/10923...
socket                    /tmp/mysql.sock           /home/mysql_3306/mysql...
sql_mode                  STRICT_TRANS_TABLES,NO... STRICT_TRANS_TABLES,NO...
ssl_ca                    ca.pem                    
ssl_cert                  server-cert.pem           
ssl_key                   server-key.pem            
thread_cache_size         18                        48
tx_isolation              REPEATABLE-READ           READ-COMMITTED
version                   5.7.10-log                5.7.11-log
version_comment           Source distribution       MySQL Community Server...
version_compile_os        Linux                     linux-glibc2.5

看到在a上是正常,b是有錯誤提示,2個實例有幾個重要的參數不一致。
sql_mode,tx_isolation,version_comment,把b的這2個參數設置一致後,也是報錯,看來不是這2個參數導致的.
查看optimizer_switch參數
show variables like '%switch%'; +------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Variable_name | Value | +------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | optimizer_switch | index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,block_nested_loop=on,batched_key_access=off,materialization=on,semijoin=on,loosescan=on,firstmatch=on,duplicateweedout=on,subquery_materialization_cost_based=on,use_index_extensions=on,condition_fanout_filter=on,derived_merge=on| +------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2邊也是一樣的,但是實例的版本是不一樣的,並且語句是自查詢更新,通過搜索文檔發現
However, beware that from MySQL 5.7.6 and onward, the optimiser may optimise out the subquery, and still give you the error. Luckily, the optimizer_switch variable can be used to switch off this behaviour; although I couldn’t recommend doing this as anything more than a short term fix, or for small one-off tasks.
嘗試關閉自查詢展開,sql可以執行,但是不建議這麼做,還是建議該sql,把sql簡化處理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章