Jump to content

SQL Queries


joer80
 Share

Recommended Posts

I am new to process wire and am planning on setting up my next few projects on it, but I have a few questions!

On a clean install that has nothing but a homepage with a title and description, about how many sql queries are we already looking at?

Also, if I want to query a database without using the api selectors, is there a way to piggyback on an open connection?  Or do I need to open a new one?  (If so, is there a process wire way to do it?)

Thanks!

Link to comment
Share on other sites

Hi joer80 and welcome to PW!

You can get a count and list of all queries on a page like this:

$queries = $database->getQueryLog();
echo '<p>Number queries: ' . count($queries) . '</p>';
foreach($queries as $query){
    echo '<p>'.$query.'</p>';
}

Just put that somewhere in your home template.

I have to run out the door, but someone should chime in with how to use your own SQL easily. There are posts about it here. Google search is much more effective than the forum's built in one.

Good luck!

  • Like 6
Link to comment
Share on other sites

Welcome joer80,

If you want to look at the query log on the admin side, just turn on debug mode by editing the /site/config.php file and changing $config->debug = false; to $config->debug = true; and saving the file. After that you get a load of debug information at the bottom of every admin page and it includes a list of the queries.

With regard to query count: MySQL has a pretty good query cache so just going by the query count might not show the whole picture with regard to where something might be bottlenecked.

When accessing the DB and running my own queries directly from modules I've never had to open a connection so either PW does it for you behind the scenes or you get to re-use what it already has open (I'm guessing the later but I haven't stepped through the code to confirm.) Here's an example query from a module I'm working on now...

$t = $this->db->query("SHOW CREATE TABLE `$table`"); 

But you can just do...

$t = $db->query("SHOW CREATE TABLE `$table`"); 

...from a template file. More details.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...