Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching parsing/analysis of Query Grammar #2816

Open
steve-chavez opened this issue Jun 9, 2023 · 2 comments
Open

Caching parsing/analysis of Query Grammar #2816

steve-chavez opened this issue Jun 9, 2023 · 2 comments
Labels
idea Needs of discussion to become an enhancement, not ready for implementation perf

Comments

@steve-chavez
Copy link
Member

Problem

There are several steps we do for each request to become a generated query. e.g. for a GET:

handleRequest :: AuthResult -> AppConfig -> AppState.AppState -> Bool -> Bool -> PgVersion -> ApiRequest -> SchemaCache -> Handler IO Wai.Response
handleRequest AuthResult{..} conf appState authenticated prepared pgVer apiReq@ApiRequest{..} sCache =
case (iAction, iTarget) of
(ActionRead headersOnly, TargetIdent identifier) -> do
wrPlan <- liftEither $ Plan.wrappedReadPlan identifier conf sCache apiReq
resultSet <- runQuery roleIsoLvl (Plan.wrTxMode wrPlan) $ Query.readQuery wrPlan conf apiReq
return $ Response.readResponse headersOnly identifier apiReq resultSet

Before that:

apiRequest <-
liftEither . mapLeft Error.ApiRequestError $
ApiRequest.userApiRequest conf req body
Response.optionalRollback conf apiRequest $
handleRequest authResult conf appState (Just authRole /= configDbAnonRole) configDbPreparedStatements pgVer apiRequest sCache

Solution

Akin to "prepared statements", implement "prepared requests". We can make a hash of our input parameters and store the generated query. Then just execute the query using the arguments.

Notes

@steve-chavez steve-chavez added perf idea Needs of discussion to become an enhancement, not ready for implementation labels Jun 9, 2023
@wolfgangwalther
Copy link
Member

Avoiding the ApiRequest step to parse the query parameters(?id=eq.3&col=lt.4) is not possible. If we switched to a grammar like ?id.eq=3&col.lt=4(#2066) it might be possible to avoid our query grammar parsing and only do some lighter parsing.

Hm, I'm not sure what your goal is. Do you want to:

  • hash the values 3 and 4, too?
  • hash only the "structure", so kind of like a "parametrized request", similar to the prepared statement?

If you wanted to hash the values, too, you could just use the whole query string for the hash, no parsing required.

If not... then you'd need to parse way too much of the actual structure of the request before you could actually calculate the hash - you'd almost be there in that case.

The SEARCH example you gave would be fine with using the full query string as input for the hash, too.

@steve-chavez
Copy link
Member Author

hash only the "structure", so kind of like a "parametrized request", similar to the prepared statement?
If not... then you'd need to parse way too much of the actual structure of the request before you could actually calculate the hash - you'd almost be there in that case.

Yes, only the structure. But as you mention that's not worth it. SEARCH looks good.

My motivation is that with #2523 and #1582 we're going to be putting more work at the Plan level. That will certainly not be zero cost in terms of perf.


#2771 will prove how much time we take for parsing/planning.

@steve-chavez steve-chavez changed the title Implement "Prepared Requests" Caching parsing/analysis of Query Grammar - Implement "Prepared Requests" Jul 14, 2023
@steve-chavez steve-chavez changed the title Caching parsing/analysis of Query Grammar - Implement "Prepared Requests" Caching parsing/analysis of Query Grammar Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Needs of discussion to become an enhancement, not ready for implementation perf
Development

No branches or pull requests

2 participants