MySql “view”, “prepared statement” and “Prepared statement needs to be re-prepared”

岁酱吖の 提交于 2019-12-24 04:54:20

问题


I've a problem with MySql, here is the details : I created a new schema/database, executed (only) this queries :

create table mytable (
id varchar(50) not null,
name varchar(50) null default '',
primary key (id));

create view myview as
select id,name from mytable;

insert into mytable values ('1','aaa');
insert into mytable values ('2','bbb');
insert into mytable values ('3','ccc');

and then, if I run these queries :

select * from mytable;
select * from myview;
prepare cmd from 'select id,name from mytable where id=?';
set @param1 = '2';
execute cmd using @param1;

the queries give the correct result (3 rows,3 rows,1 row).

but, the problem exists if I run this query:

prepare cmd from 'select id,name from myview where id=?';
set @param1 = '2';
execute cmd using @param1;

ERROR: #1615 - Prepared statement needs to be re-prepared

I've done some research and found that the increment of configurations below "may" solve the problem :

increase table_open_cache_instances value
increase table_open_cache value
increase table_definition_cache value

As far as I know, the queries above are the common and standard MySql queries, so I think there is no problem with the syntax.

I'm on a shared webhosting and using MySql version is 5.6.22

But the things that make me confused is, it only contain 1 schema/database, with 1 table with 3 short records and 1 view, and I executed a common and standard MySql select query, does the increment of values above really needed? is there anyone with the same problem had increase the values and really solve the problem? or, perhaps do you have any other solution which you think may or will works to solve this problem?

ps: it does not happen once or twice in a day (which assumed caused by some backup or related), but in all day (24 hours).

Thank you.


回答1:


Do you do this after each execute?

deallocate prepare cmd;



回答2:


The closest guess until now is some other shared members on the server dont write their code quite well (because it is a shared webhosting), either doing large alter while doing the large select, or dont deallocate the prepared statement after using it, like Rick James said. (want to make the post usefull, but I dont have the reputation, sorry Rick)

I can not make sure if the increment of "table_definition_cache" will works because the system administrator still wont change the value until now, but incase you having the same problem and you can modify it, it worth to try.

My current solution is I change all my views in my query strings into non-view or subqueries, it works for me, but the problem is still in the air.

eg. from

select myview.id, myview.name
from myview
inner join other_table on ...
where myview.id=?

into

select x.id, x.name
from (select id,name from mytable) x
inner join other_table on ...
where x.id=?


来源:https://stackoverflow.com/questions/29480915/mysql-view-prepared-statement-and-prepared-statement-needs-to-be-re-prepar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!