N1QL vs Python SDK Performance and get , get_multi clarification

Hi Team ,
I am trying to use get and get_multi CB 2.5.7 Python API queries to fetch data .
I can pass keys inside it and get the data I need . But is there any way I can pass arguments or parameters on this SDK functions to get the desired data that I want ? Basically I want to filter the data for certain parameters .
I read SDK documentation and I feel its very hard to find the desired information that I need

https://docs.couchbase.com/sdk-api/couchbase-python-client-2.5.7/api/couchbase.html?highlight=get%20function

Also my last question is what is the performance difference if we use SDK API call like get , get_multi vs when we use N1QL query ?

Thanks

Hi, this would typically be a use case for N1QL rather than directly running on the Python client.

You can of course filter the dictionary contents returned by get_multi but the N1QL server will typically have far better performance, as it is written in Golang and is located directly on the cluster, so nearer the data.

Not sure if we have any benchmarks but this would be our rationale performance-wise for using N1QL over native Python filtering.

If the size of the dictionary returned is a concern but you still want to filter results with Python, you could use an async API to process results as they come in.

Hope that helps,

Ellis

thanks for the information and makes it clear. Can you please help understanding how I will be using get or get_multi with predicates rather only keys . I am not getting good details on SDK documentation about how should we pass arguments on get and get_multi . Any pointer would be great .
thanks again

Hi,

As stated, you would be better off using N1QL for this, unless the predicates you wanted aren’t provided by N1Ql. The n1ql_query method on couchbase.bucket.Bucket should do this for you.

There is no option to pass a filtering predicate to get_multi - you would have to post-filter the results, either in whole with the synchronous (default) API:

def is_list(key, value):
    return isinstance(value,list)

def filter_keys(cb, predicate):
    x=cb.get_multi(keys)
    filtered={k:rv.value for k,rv in x.items() if predicate(k,rv.value)}

list_entries=filter_keys(cb, is_list)

or as they come in with the asynchronous API.

Thanks,

Ellis