Hi,
In Couchbase Server 4.0, will there the equivalent of stored procedures in N1QL, or will all queries have to be parsed completely each time they are sent to the query services?
Thanks,
Marcus.
Hi,
In Couchbase Server 4.0, will there the equivalent of stored procedures in N1QL, or will all queries have to be parsed completely each time they are sent to the query services?
Thanks,
Marcus.
There is a feature that comes close to that, which is called âprepared queriesâ. This is not really server-side, but allows you to skip the parsing phase of a query. It works this way:
PREPARE
like this: PREPARE SELECT * FROM default
SELECT * FROM default
.prepared
parameter instead of classical statement
. The value of prepared is the plan you got earlier.Thatâs it!
In the Java SDK, this is supported by calling Bucket
's prepare
method (prefixing your statement for you) and the result is wrapped in a QueryPlan
object that you can cache and reuse.
Execute it through a PreparedQuery
:
myBucket.query(Query.prepared(plan, parameters))
.Net has also a very similar API.
Thanks for your response. A few more questions:
I wonât be using the Java SDK or .Net. If Iâm accessing the N1QL interface using HTTP, will that work as well?
Does the produced query plan include all the indexes that it will use?
Is it possible to update the query plan by using the same syntax?
Thanks again.
yes, all of this is possible. In the documentation, you can find a pdf with the N1QL reference. http querying is described there in more details that what I provided above, including how to do prepared queries.
IIRC the query plan (quite verbose) includes the indexes to use, and it can be updated by just redoing the PREPARE statement and replacing the old plan by the new one (eg. in case you create an index in the meantime).
Just after your previous reply I found the âpreparedâ information in the documentation. However, I couldnât find any examples of prepared queries, either in the online docs or the 100+page PDF download. Maybe they are hidden somewhere, but they didnât appear to be in the obvious place with the other REST API calls.
Any chance you could post a simple example or point me to a page where itâs fully documented?
Thanks.
Hi Eugaia,
please see here for an example of using a prepared statement:
Hope this helps.
Thanks,
Colm.
@colm Perfect, thatâs just what I needed. Thanks.
I see that the prepared statement thatâs provided is the full JSON, and not just a named query. Are there any plans to store named queries on the server-side that are basically cached versions of the prepared query?
Thanks.
Hi @Eugaia,
do you mean named query per Java Persistence API? There is no plan to implement named queries per se, but there is awareness (and a plan) for adding to prepared statements:
https://issues.couchbase.com/browse/MB-13643
In addition, prepared statements are cached server-side (using a hash of the JSON).
Thanks,
Colm.
I was not referring so much to caching named queries per Java Persistence API, but more the latter of what you said (i.e. server-side cached prepared statements). I was initially thinking of the user atrributing a name, but a hash I think would work better in practice as you suggest.
What you talk of with there sounds very much how Lua embedded in Redis works (from what I remember).
Are you planning on doing something like this:
As a matter of interest, what hashing method are you intending on using to avoid any possible hash conflicts?
Thanks,
Marcus.
Hi @Eugaia,
thank you for the great suggestions; weâll make a point of investigating Redis (as well as other systems that offer a similar feature, I believe Aerospike supports Lua stored procedures).
Currently with N1QL, the client sends the prepared statement, the server hashes this and uses the hashed value to retrieve the prepared object; if it is not present then the server creates a prepared object from the clientâs input. Per Loading... we intend to streamline this process and will implement something along the lines of what you describe. Really appreciate you taking the time to share your ideas.
Thanks,
Colm.
@colm Youâre welcome.
Hi @Eugaia,
We now have named prepared statements in N1QL, as well as named + encoded prepared statements. Please see the latest documentation.
@geraldss Thanks for your reply to this (which Iâve just seen). I shall check this out.
@simonbasle - Need your help hereâŚ
In the above explanation point 4.
ââ for this submit a query with a prepared parameter instead of classical statement. The value of prepared is the plan you got earlier. ââ
1.) Have created plan using the prepared statement.
prepare select a.function_section.* from ace_supplier a where a.type = $function;
2. ) I got the below plan name.
68aad199-f7b5-4b67-867d-bb6398ccbc67
3.) Then I would like to use this plan in my JAVA code without REST API.
4.) Kindly advice me how I can do this using.
It would be really great if you can able to share the codeâŚ
myBucket.query(Query.prepared(plan, parameters))âŚ
myBucket - mybucket.
query - which method we have to select. have tens of methods.
Query - What it is actually�
Hi simonbasle,
i have query here, i am inserting data from a java application to the database(bucket) whenever a specific value triggered i want to do an action based on that triggering point , we have implemented the same in postgres using stored procedure. how can we implement this in couchbase , please suggest a solution. any help would be appreciated.
Thank You
Hey @kmurali, itâd be best if you post a new thread rather than pick up a 10 month old one!
Have a look at the functions feature in Couchbase Server 5.5.