Mysql query showing different results - php

I want to count total records in the table. My table contains 1 million records, so I'm using
EXPLAIN
SELECT COUNT(id) FROM table_name
instead of
SELECT COUNT(id) FROM table_name
for faster retrieval.
If I'm using SELECT COUNT(id) FROM table_name means I'm getting proper count. EXPLAIN SELECT COUNT(id) FROM table_name gives wrong count. I have reduced the record count to 10000, But still I can't find the issue.
Here is my Query
EXPLAIN SELECT COUNT(id) FROM table_name - 12764 - wrong
SELECT COUNT(id) FROM table_name - 10000 - right

Explain is not an optimization it is a description of the execution plan for the specified statement. This explanation is returned as a table. And this is what you are counting.
Therefore you are counting completely different things.
From the MySQL docs:
EXPLAIN is used to obtain a query execution plan (that is, an explanation of how MySQL would execute a query).
As explain select can return the rows affected. You may want to use something like
EXPLAIN
SELECT SUM(rows)
FROM table_name
GROUP BY (table, rows)

Related

Count occurence of id from mysql select and combine duplicate with php (not in mysql query, distinct, grouby)

I have some mysql results as follows:
ID
782gt
782gt
782gt
997fd
The end goal is to have:
an array:
'997fd ' => "1"
'782gt' => "3"
Ive tried,
(array_count_values($row));
But this does not total and combine values. This is for a high load situation so I am avoiding distinct and groupby in the actual query, tempory table and so on...
You mentioned that this may be a high data load operation, and you are trying to avoid using GROUP BY in your query. Yet, the database is really the best place to be doing this calculation. So I recommend a simple GROUP BY query:
SELECT ID, COUNT(*) AS cnt
FROM yourTable
GROUP BY ID;

Duration between type in and out in MYSQL

I am new MYSQL now i try something here is my query not accurate result
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time))) FROM `officialbreaks` where type='out'
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time))) FROM `officialbreaks` where type='in'
Any buddy have work on that type of situation.
OUT REQUIRED
TIMEDIFF(time where type=in, time where type=out)
3:35:30 time type=in, 03:35:30 time type=out
Output 0
You can try as per below-
SELECT DISTINCT a.userid, (TIME_TO_SEC(b.time) - TIME_TO_SEC(a.time)) AS 'time_diffrence' FROM
(SELECT userid,`time` FROM mytable WHERE `type`='in') a
JOIN (SELECT userid,`time` FROM mytable WHERE `type`='out') b ON a.userid=b.userid
It is a simple solution but there can be multiple out time against single in time and multile users etc. so there can be so many combination where query need to change.

Determine if SQL UPDATE affected individual JOINed tables

According to http://us3.php.net/manual/en/pdostatement.rowcount.php:
PDOStatement::rowCount() returns the number of rows affected by the
last DELETE, INSERT, or UPDATE statement executed by the corresponding
PDOStatement object.
Using a single query, is it possible to tell if an individual JOIN'd table was affected? For instance, given the following query, how would I know if t1 was affected and if t2 was affected?
$sql ='UPDATE t1 INNER JOIN t2 ON t2.t1_id=t1.id SET t1.foo=:foo, t2.bar=:bar WHERE t2.id=:id';
$stmt = db::db()->prepare($sql);
$stmt->execute(array('foo'=>123,'bar'=>321,'id'=>10));
$rows_t1=$stmt->rowCount();
$rows_t2=$stmt->rowCount();
The UPDATE_TIME column in the information_schema.tables table approximately answers the question "which table was updated". Basic example:
SELECT UPDATE_TIME
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'database' AND TABLE_NAME = 'table'
If you were to run this right after your modifying statements, you could limit to a window of a few seconds to check if a particular table was updated, like:
SELECT COUNT(*)
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'database' AND TABLE_NAME = 'table'
AND UPDATE_TIME BETWEEN (NOW() - INTERVAL 30 SECOND) AND NOW();
which returns 1 if that table was updated in the last 30 seconds, or 0 if not.
I stress this is an approximate answer, because a query other than the one you last executed might have affected that table. If you were to wrap this in a transaction or a lock, then you could use this to actually answer your question: with the cost of write-locking other connections.
INNER JOIN ensures you only get results if a match is found on both tables.
Which means if the database can not match a result in one of the tables then the row is not included in the result set.
So the number of results returned from stmt->rowCount(); will reflect updates only on both tables.

limit vs exists vs count(*) vs count(id) in MySQL [duplicate]

This question already has answers here:
Is EXISTS more efficient than COUNT(*)>0?
(5 answers)
Closed 1 year ago.
I just want to know which one is the fastest.
What I'm trying to do is to just check if the data is existing on the table.
I've been using "LIMIT" most of the time but in your opinion or if you have basis, which one is the fastest to check if data is existing.
Example:
limit 1:
SELECT ID
FROM TABLE
WHERE ID=1 LIMIT 1;
exists:
SELECT EXISTS(
SELECT *
FROM TABLE
WHERE ID=1);
count(*):
SELECT (*)
FROM TABLE;
count(ID):
SELECT (ID)
FROM TABLE;"
Additional: I'm using InnoDB.
Limit is always the fastest, because it iterate one line of the table.
Exists has little difference with Limit because you just add another select statement, we can say it has the same efficiency as the first one.
Count will iterate all the table and count the result. When you use count(), by default, mysql count the primary key of the table. I've done some tests of count(id), count(), count(field) and count(1) in big table, there is no big difference. In my opinion, 'count' will always try to count the index unless the field you count is not an index, but many people said that we should use count(id) rather than use count(*).
In a small table, the four ways all work fine. But if you join with some big table, count will take a very very long time.
So in all, the time used is count(*) > count(id) >> exists > limit
I think they are all fine; except I would remove the WHERE ID = 1 clauses. If you ever clear your table and start re-inserting then ID 1 will not exist. Just LIMIT 1 will do the trick. Personally I don't favour the exists and count(*) options. I would prefer count(ID) then, as you would normally have an index on ID so I would expect that to run fairly quickly. To be sure, you would have to time them (on a really big table) - I expect them to come out something like exists, limit 1, count(id), count(*) from fastest to slowest. (I am in doubt about the exists though - if it actually evaluates the whole SELECT * it may come out worst).

Optimize MySQL query order in the results using GROUP BY and count

I have the following query:
SELECT vBrowser,iconBrowser, count(iconBrowser) as 'N'
FROM user_ip_tmp WHERE code='9m9g9tsv2y'
GROUP BY iconBrowser
ORDER BY N DESC
LIMIT 40
And this works properly. But the delirious cause query took a long time.
Showing rows 0 - 17 ( 18 total, Query took 4.4189 sec)
Things that are in WHERE statement, should be indexed.
Try to use EXPLAIN statement before your SELECT to see what and how is used to retrief your requested results.
And if the column code is not an unique value, i would recommend to put it in some other table, where it is unique. Then build the query using JOIN though the FOREIGN KEY.

Categories