MySQL视图查询报错:Prepared statement needs to be re-prepared
From:?https://www.ywnds.com/?p=12609
今天公司的項(xiàng)目視圖查詢報(bào)錯(cuò),報(bào)錯(cuò)如:ERROR 1615 (HY000): Prepared statement needs to be re-prepared
網(wǎng)上找了一圈,都說(shuō)調(diào)整以下值就好了:
mysql> set global table_open_cache=16384; mysql> set global table_definition_cache=16384;其實(shí)真正只調(diào)整table_definition_cache問(wèn)題就解決了。這兩個(gè)參數(shù)是MySQL中與表對(duì)象緩存相關(guān)的參數(shù)。
table_open_cache
| System Variable | Name | table_open_cache | |
| Scope | Global | ||
| Dynamic | Yes | ||
| Permitted Values | Type | integer | |
| Default | 2000 | ||
| Minimum | 1 | ||
| Maximum | 524288 | ||
打開表的緩存數(shù)量。也不是定義內(nèi)存大小的,而是定義可以緩存多少打開的表的文件句柄信息。如果定義的太小,那么MySQL在需要打開新表的時(shí)候就要不斷的關(guān)閉已經(jīng)打開的表和打開此次需要打開的表。性能會(huì)受到影響。
table_definition_cache
| System Variable | Name | table_definition_cache | |
| Scope | Global | ||
| Dynamic | Yes | ||
| Permitted Values | Type | integer | |
| Default | -1 (autosized) | ||
| Minimum | 400 | ||
| Maximum | 524288 | ||
表定義信息緩存是從MySQL 5.1.3版本才開始引入的一個(gè)新的緩存區(qū),用來(lái)存放表定義信息。當(dāng)我們的MySQL中使用了較多的表的時(shí)候,此緩存無(wú)疑會(huì)提高對(duì)表定義信息的訪問(wèn)效率。MySQL提供了table_definition_cache參數(shù)給我們?cè)O(shè)置可以緩存的表的數(shù)量。在MySQL 5.1.25之前的版本中,默認(rèn)值為128,從MySQL 5.1.25版本開始,則將默認(rèn)值調(diào)整為256了,最大設(shè)置值為524288。注意,這里設(shè)置的是可以緩存的表定義信息的數(shù)目,而不是內(nèi)存空間的大小。
然后就是這個(gè)兩個(gè)參數(shù)跟視圖訪問(wèn)報(bào)錯(cuò)有什么關(guān)系呢?有點(diǎn)懵逼了,就問(wèn)了一個(gè)朋友@八怪,看了一下這個(gè)報(bào)錯(cuò)的錯(cuò)誤碼。
{ “ER_NEED_REPREPARE”, 1615, “Prepared statement needs to be re-prepared” }
分析后他應(yīng)該是在函數(shù)check_and_update_table_version中拋出來(lái)的。
static bool check_and_update_table_version(THD *thd,TABLE_LIST *tables, TABLE_SHARE *table_share) { if (! tables->is_table_ref_id_equal(table_share)){Reprepare_observer *reprepare_observer= thd->get_reprepare_observer(); if (reprepare_observer &&reprepare_observer->report_error(thd)) //這里如果前面的指針為NULL則觸發(fā)這個(gè)報(bào)錯(cuò)邏輯 { /*Version of the table share is different from theprevious execution of the prepared statement, and it isunacceptable for this SQLCOM. Error has been reported.*/ DBUG_ASSERT(thd->is_error()); return TRUE;} /* Always maintain the latest version and type */ tables->set_table_ref_id(table_share);}DBUG_EXECUTE_IF("reprepare_each_statement", return inject_reprepare(thd);); return FALSE; }看來(lái)他們確實(shí)有聯(lián)系,但是怎么聯(lián)系的說(shuō)不上來(lái),這個(gè)觀察者搞毛線的我也不知道。以后再說(shuō)。斷點(diǎn)設(shè)置
breakpoint keep y 0x0000000000ebd5f3 in main(int, char**) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25 breakpoint already hit 1 time 4 breakpoint keep y 0x00000000016a04bd in open_table_from_share(THD*, TABLE_SHARE*, char const*, uint, uint, uint, TABLE*, bool)at /root/mysql5.7.14/percona-server-5.7.14-7/sql/table.cc:3038 breakpoint already hit 4 times 5 breakpoint keep y 0x0000000001519a10 in check_and_update_table_version(THD*, TABLE_LIST*, TABLE_SHARE*)at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_base.cc:4219 breakpoint already hit 4 times 6 breakpoint keep y 0x00000000015285bb in Table_cache::add_used_table(THD*, TABLE*) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/table_cache.h:353 breakpoint already hit 2 times 7 breakpoint keep y 0x0000000001527427 in TABLE_LIST::set_table_ref_id(enum_table_ref_type, ulonglong)at /root/mysql5.7.14/percona-server-5.7.14-7/sql/table.h:2100 8 breakpoint keep y 0x00000000015273e3 in TABLE_LIST::set_table_ref_id(TABLE_SHARE*) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/table.h:2095 breakpoint already hit 1 timetable_open_cache與table_definition_cache對(duì)MySQL(內(nèi)存)的影響
總結(jié)
以上是生活随笔為你收集整理的MySQL视图查询报错:Prepared statement needs to be re-prepared的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 实战使用Axure设计App,使用Web
- 下一篇: 数据库调优要点纪要