I have several SELECT statements on a PHP page, and I used Dreamweaver to generate those.
After going through the code it generated, there seemed to be alot of fluff which I could cut out under most circumstances, a mysql_num_rows() line for each statement being an example.
So I'm wondering if anyone can tell me whether or not this actually saves resources - considering the query is being run regardless, is there any actual overhead for this?
UPDATE:
After following Chriszuma's suggestion about microtime, here are my results:
//time before running the query
1: 0.46837500 1316102620
//time after the query ran
2: 0.53913800 1316102620
//time before calling mysql_num_rows()
3: 0.53914200 1316102620
//time after mysql_num_rows()
4: 0.53914500 1316102620
So not much overhead at all, it seems
mysql_num_rows() counts rows after they have been fetched. It's like you fetched all rows and stored them in a PHP array, and then ran count($array). But mysql_num_rows() is implemented in C within the MySQL client library, so it should be a bit more efficient than the equivalent PHP code.
Note that in order for mysql_num_rows() to work, you do have to have the complete result of your query in PHP's memory space. So there is overhead in the sense that a query result set could be large, and take up a lot of memory.
I would expect that such a call would have an extremely minimal impact on performance. It is just counting the rows of its internally-stored query result. The SQL query itself is going to take the vast majority of processing time.
If you want to know for sure, you can execute microtime() before and after the call to see exactly how long it is taking.
$startTime = microtime(true);
mysql_num_rows();
$time = microtime(true) - $startTime;
echo("mysql_num_rows() execution: $time seconds\n");
My suspicion is that you will see something in the microseconds range.
Related
I have a summary query where all result of the calculation are showed. I store only the data and on every request a function calculates the result. My problems is this method causes waiting times. I would like to reduce it. The best thing I could do is to cache the results for 24 hours.
I can not store the results, because many of the variables has to be changed easily.
What would be the best practice to run php functions on mysql select query?
This was a typo question and I didn't really want to leave my code up here. It would not benefit anyone else.
This is my code and I want the variable $x to be itself plus the value of $points. For some reason, this is the output I get:
TOTAL Before Add: 0
points: 8
TOTAL after add: 8
TOTAL Before Add: 0
points: 32
TOTAL after add: 32
I want this to make x: 40 (adding 8+32).
Why is x starting over at 0 again every time?
Appreciate the help,
R
In the code, you confuse $row3 and $row4, but I'll assume you're iterating over one row here. Also, you seem to present a heavily abstracted version here. Verify that the bug appears even after the simplification, for example with a test database. But assuming that's not the case, the only explanation is:
You're executing the whole code snippet multiple times. Each time it is executed, it reads exactly one line. Add
echo "Start of loop, x: $x";
before the while and you'll see two runs.
As a final note, you should really only use PDO in post-2010 code. It is database-independent and has excellent support for transactions and prepared statements.
In response to the edit:
First of all, your code is vulnerable to SQL injection. Fix that now, for example by using PDO prepared statements.
Secondly, why are you joining in php code? You should calculate the sums and all the data you need on the database, with multiple JOINs. You should not need more than one(maybe two) SQL query for that.
Thirdly, the problem is that you're not adding to $total:
$total=$Total+$points;
Since $Total is undefined, it's evaluated as 0, so you could equally write:
$total = $points;
Of course, you wanted:
$total += $points;
But as I mentioned above, it's still an extremely bad coding style to perform trivial calculations (such as summing up) in the application instead of the database.
I have a table in my database that has about 200 rows of data that I need to retrieve. How significant, if at all, is the difference in efficiency when retrieving all of them at once in one query, versus each row individually in separate queries?
The queries are usually made via a socket, so executing 200 queries instead of 1 represents a lot of overhead, plus the RDBMS is optimized to fetch a lot of rows for one query.
200 queries instead of 1 will make the RDBMS initialize datasets, parse the query, fetch one row, populate the datasets, and send the results 200 times instead of 1 time.
It's a lot better to execute only one query.
I think the difference will be significant, because there will (I guess) be a lot of overhead in parsing and executing the query, packaging the data up to send back etc., which you are then doing for every row rather than once.
It is often useful to write a quick test which times various approaches, then you have meaningful statistics you can compare.
If you were talking about some constant number of queries k versus a greater number of constant queries k+k1 you may find that more queries is better. I don't know for sure but SQL has all sorts of unusual quirks so it wouldn't surprise me if someone could come up with a scenario like this.
However if you're talking about some constant number of queries k versus some non-constant number of queries n you should always pick the constant number of queries option.
In general, you want to minimize the number of calls to the database. You can already assume that MySQL is optimized to retrieve rows, however you cannot be certain that your calls are optimized, if at all.
Extremely significant, Usually getting all the rows at once will take as much time as getting one row. So let's say that time is 1 second (very high but good for illustration) then getting all the rows will take 1 second, getting each row individually will take 200 seconds (1 second for each row) A very dramatic difference. And this isn't counting where are you getting the list of 200 to begin with.
All that said, you've only got 200 rows, so in practice it won't matter much.
But still, get them all at once.
Exactly as the others have said. Your RDBMS will not break a sweat throwing 200+++++ rows at you all at once. Getting all the rows in one associative array will also not make much difference to your script, since you no doubt already have a loop for grabbing each individual row.
All you need do is modify this loop to iterate through the array you are given [very minor tweak!]
The only time I have found it better to get fewer results from multiple queries instead of one big set is if there is lots of processing to be done on the results. I was able to cut out about 40,000 records from the result set (plus associated processing) by breaking the result set up. Anything you can build into the query that will allow the DB to do the processing and reduce result set size is a benefit, but if you truly need all the rows, just go get them.
I am working on a site at the moment, and there is a concentrated focus on efficiency and speed in loading, processing and such like.
I'm using the mysqli extension to get my database bits and bobs, but I'm wondering what's the best / most efficient way of outputting my dataset?
At the moment I'm using $mysqli->fetch_assoc() and a foreach(). Having read http://www.phpbench.com I know that counting my data first makes a difference. (I'm going to optimise after build)
My question is, which is quicker for getting a resultset into a php data thing. Creating an object? A numerical array? An associative array? My thoughts are an object, but I'm unsure.
Just curious, as I'm not familiar with the PHP internals :)
There is actually a small benchmark in PHP documentation under mysql_fetch_object's comments:
SELECT * FROM bench... (mysql_fetch_object)
Query time: 5.40725040436
Fetching time: 16.2730708122 (avg: 1.32130565643E-5)
Total time: 21.6803212166
SELECT * FROM bench... (mysql_fetch_array)
Query time: 5.37693023682
Fetching time: 10.3851644993 (avg: 7.48886537552E-6)
Total time: 15.7620947361
SELECT * FROM bench... (mysql_fetch_assoc)
Query time: 5.345921278
Fetching time: 10.6170959473 (avg: 7.64049530029E-6)
Total time: 15.9630172253
Fetching an object is slowest, fetching a numeric array is probably a bit faster than using mysql_fetch_array or mysql_fetch_assoc, but the difference is negligible. In fact, mysql_fetch_array fetches both assoc and numeric, and it's faster than mysql_fetch_assoc, go figure.. But if you're after performance, just don't use mysql_fetch_object.
From the manual page of mysql_fetch_object()
Note: Performance
Speed-wise, the function is identical to mysql_fetch_array(), and almost as quick as
mysql_fetch_row() (the difference is insignificant)
As the benchmark given by Tatu suggests, there is a slight difference, but keep in mind the numbers have been cumulated from 100 consecutive queries. I'd say your strategy of not bothering now and optimize later is a good choice.
I believe a numerical array is likely to be the most lightweight, followed by associative array, and then an object. I don't think the differences will add up to much, so whichever syntax you're most comfortable with is best.
Which do you think is faster in a PHP script:
$query = "SELECT... FROM ... ORDER BY first_val";
or
while($row = odbc_fetch_array($result))
$arrayname[] = array(
"first_key" => $row['first_val'],
"second_key" => $row['second_val'],
etc...
);
sort($arrayname);
It depends on so many factors that I don't even know what to begin with.
But as a rule, you perform sorting on database side.
Indexes, collations and all this, they help.
Which do you think is faster in a php script:
The ORDER BY doesn't execute in the PHP script -- it executes in the database, before data is retrieved by the PHP script. Apologies if this seems pedantic, I just want to make sure you understand this.
Anyway, the reason I would use ORDER BY is that the database has access to indexes and cached pages from the database. Sorting in PHP sorts the data set in memory of course, but has no access to any index.
If the ordered field is indexed, I'd say probably the SQL query. If not, I'm not sure, but I can't imagine it will be overly noticeable either way unless you're dealing with an absurdly large number of rows.
ORDER BY will almost always be faster.
In my opinion, nothing beats actually timing the thing so you really, really know for sure:
$time_start = microtime(true);
// Try the ORDER BY and sort($array) variants here
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "It took $time seconds";
If there's a LIMIT on the first query, and the set of rows the query would match without the LIMIT is much larger than the LIMIT, then ORDER BY on the query is DEFINITELY faster.
That is to say, if you need the top 50 rows from a 10,000 row table, it's much faster to have the database sort for you, and return only those top 50 rows, than it is to retrieve all 10,000 rows and sort them yourself in PHP. This is probably representative of the vast majority of what will happen in real-world applications
If there are any cases at all in which sorting in PHP is even comparable, they're few and far between.
Additionally, SQL sorting is much more powerful -- it's trivial to sort on multiple columns, subqueries, the return values of aggregate functions etc.