Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
957 views
in Q2A Core by

From qa-db-selects.php it can be read:

    Return the results of all the SELECT operations specified by the supplied selectspec parameters, while also
    performing all pending selects that have not yet been executed. If only one parameter is supplied, return its
    result, otherwise return an array of results indexed as per the parameters.

 

Can somebody clarify when we should use this function?

Thanks
Kai

by
I have the same question in my mind .

1 Answer

+2 votes
by
selected by
 
Best answer
In short, the qa_db_select_with_pending function executes all the supplied queries (provided as "selectspecs" not raw queries) and any queued queries. There is generally no reason to use this yourself in plugins.

When a page is loaded there are some queries that are "queued" early on, before we get to the actual specific code for a page. The main example is the options which are fetched on every page.

It's done like that for when the "distant database" option is set in the config - all queries relevant to a page are run at once which is faster when there is latency. Most people will have MySQL on the same server as PHP so should set the "local database" option - in this case each query is run separately which is faster in that situation.

The qa_db_select_with_pending function is run on practically every page. Once it has been run, there is no benefit to running it again since the pending queries have been executed. If you have several queries you need to run you can call direct to qa_db_multi_select instead with an array of selectspecs. It will then be a little faster for users with the "distant db" option. (A selectspec is an array of data about the query - see the comment above qa_db_single_select in qa-db.php for details.)
...