shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Is there any good way to handle with parsing sql first time, which is taking too many time?

Open HongJinFeng opened this issue 7 years ago • 7 comments

Hi, we use encrypt-jdbc in our project, and last night we released it to production environment,when it started at the first time, our dubbo interface had many delay warings. After my investigation, it was because parse sql took too many time, most of them took more than one second, some beyond one second. And in this situation, our interface already time out, and it may cause dubbo interface be degraded(our project has more than 500 thousand requests per minute ).

Although i had warm up some interface to cache sql statement, but the problem still exist. So, it there any good way to deal with it?I tell my leader if i can add the sql statement to redis,and every time the program start , i load it to local memory. But, he deny it, with the reason that the common module(which i package sharding-core, fix some bugs, and add some features) must with less dependencies. So i am very anxious, please help me, it there any good way to deal with this problem. @terrymanu da lao,any suggestions?

HongJinFeng avatar Dec 06 '19 08:12 HongJinFeng

Because parse SQL by ANTLR is slow, and ShardingSphere save the 1st parse result into cache. Please see details here: https://shardingsphere.apache.org/document/current/en/features/sharding/principle/parse/

I don't think we can enhance the performance before the AST into cache. If you want, you can consider about preheat your SQL.

terrymanu avatar Dec 06 '19 10:12 terrymanu

I have readed the document before and knew ANTLR is slow. Our project has hundreds SQL, and many SQL like 'select...in (...)' or 'insert...values(...)',and the number of parameters is different, so it has thousands different situations. It is impractical to preheat so many SQL (preheat every SQL need serval seconds). So any other way to handle with it?

I think if the SQL contain the table which need sharding or encrypt some column, it can parse SQL by ANTLR, but for other table, maybe it is not need parse by ANTLR but with druid, or others. Or is there any more elegant way complete it?

HongJinFeng avatar Dec 06 '19 13:12 HongJinFeng

Only use preparedStatement can be cached, please use parameter marker to instead of parameter inline is your SQL

terrymanu avatar Dec 07 '19 08:12 terrymanu

I think if the SQL contain the table which need sharding or encrypt some column, it can parse SQL by ANTLR, but for other table, maybe it is not need parse by ANTLR but with druid, or others. Or is there any more elegant way complete it?

No one can afford taking control two different SQL parser engines. If you interesting develop a new SQL parser engine, maybe we can talk about pull request.

terrymanu avatar Dec 07 '19 08:12 terrymanu

I have readed the document before and knew ANTLR is slow. Our project has hundreds SQL, and many SQL like 'select...in (...)' or 'insert...values(...)',and the number of parameters is different, so it has thousands different situations. It is impractical to preheat so many SQL (preheat every SQL need serval seconds). So any other way to handle with it?

I think if the SQL contain the table which need sharding or encrypt some column, it can parse SQL by ANTLR, but for other table, maybe it is not need parse by ANTLR but with druid, or others. Or is there any more elegant way complete it?

Why no need to parse by Antlr? Could you give the performance about the comparsion among the parse engine you mentioned above?

SteNicholas avatar Dec 07 '19 13:12 SteNicholas

I think if the SQL contain the table which need sharding or encrypt some column, it can parse SQL by ANTLR, but for other table, maybe it is not need parse by ANTLR but with druid, or others. Or is there any more elegant way complete it?

No one can afford taking control two different SQL parser engines. If you interesting develop a new SQL parser engine, maybe we can talk about pull request.

It sounds that there is not better solution for this issue. I don't know is there any other effect if i change the source code like this: the logic sql is not contain the table which need sharding or encrypt some column, i create prepared statement by logic sql. image

HongJinFeng avatar Dec 09 '19 01:12 HongJinFeng

Because parse SQL by ANTLR is slow, and ShardingSphere save the 1st parse result into cache. Please see details here: https://shardingsphere.apache.org/document/current/en/features/sharding/principle/parse/

I don't think we can enhance the performance before the AST into cache. If you want, you can consider about preheat your SQL.

How to preheat SQL? Any examples?

xfyang1989 avatar Dec 09 '19 10:12 xfyang1989