how to obtain the explain result in mysql by code? - mysql

I planned to use rows in the explain result in replace of count(*). but I cannot get the rows number by code.
I have tried to treat the result as a table and select rows from it but it didn't work.

Related

SELECT COUNT with LIKE gives strange result

I have a MYSQL-database with a table that has a mediumtext column. When trying to count the number of rows that has a specific word within that column, I get some strange result.
When I do this;
SELECT COUNT(*) AS antal FROM ot_pages where otp_ocr_raw LIKE '%vass%'
I get a count of around 860.
If I instead do a simple SELECT with the same question, like this:
SELECT * FROM ot_pages where otp_ocr_raw LIKE '%vass%'
I get a recordset with 70 rows back. Why is that? What would a correct SELECT COUNT-query look like to get a count of just 70?
Ok, I found the solution. The query contained letters like ÅÄÖ.
When I preformed a ordinary SELECT-query, those letters where correctly interpreted. But when doing a SELECT COUNT() those letters seems to have been interpreted by MySQL as AAO instead, hence giving a higher count than actual rows in the recordset.
The solution was to use utf8mb4 as charset, both in the script and the actual column. Now everything seems to work.

How to get rows count in sql result with group by

I would like to know how is possible to get rows count in result of sql statement with group by. As I know count aggregation counts the result of grouped rows but I would like to know the count of all rows in result but without subselect. Does anybody know how PhpMyAdmin count rows returned by query with grouped rows? I think there is a special command for mysql but dont remember the name.

SQL discrepancy using COUNT command

I am trying to do my first steps with SQL. Currently I am trying to analyse a database and stepped over a problem which I can't explain. Eventually someone could give me a hint.
I have a mySQL table ('cap851312') witch has 330.178 table rows. I already imported the table to Excel, and verified this number!
Every single row includes a field (column 'ID_MES_ANO') for the entries date. For the time being, all the date is uniquely set "201312".
Starting the following command, I would expect to see as a result the given number of rows, however the number which appears is 476.598.
SELECT movedb.cap851312.ID_MES_ANO, count(*)
FROM movedb.cap851312;
I already imported the file to Excel, and verified the number of lines. Indeed, it is 330.178!
How could I find out, what exactly is going wrong?
Update:
I've tried:
SELECT count(*) FROM movedb.cap851312
This returns as well 476.598.
As I am using workbench, I easily could confirm the numer of 330.178 table rows.
Update 2:
The Workbench Table Inspector confirms: "Table rows: 330178"
Solved - However unsure why:
I changed the statement to
SELECT count(ID_MES_ANO) FROM movedb.cap851512;
This time the result is 330178 !
COUNT(*) counts all rows.
COUNT(ID_MES_ANO) counts only rows for which ID_MES_ANO is not null.
So the difference between the two are the rows where ID_MES_ANO is null.
You can verify this with
SELECT count(*) FROM movedb.cap851512 WHERE ID_MES_ANO IS NULL;
By the way:
SELECT movedb.cap851312.ID_MES_ANO, count(*) FROM movedb.cap851312;
means: aggregate all rows to one single result row (by using the aggregate function COUNT without GROUP BY). This result row shows ID_MES_ANO and the number of all records. Standard SQL does not allow this, because you don't tell the DBMS which ID_MES_ANO of those hundreds of thousands of records to show. MySQL violates the standard here and simply picks one ID_MES_ANO arbitrarily from the rows.

Mysql - Search for a word in json text and only give back a result if it occurs in more rows

Hy i want to search for a word in my table but i only want a result if the word occurred in more then 3 rows.
So basically my query is this, which partially works:
SELECT *
FROM `someTable`
WHERE `field_A` LIKE '%wordToFind%'
OR `field_B` LIKE '%wordToFind%'
This works just fine, in the result i will see all the rows that matched the query.
So the problem is it will return all the matched rows, for example if only one row was matched it will return only one, but i want this query to be limited to at least 5.
By that i mean i only want a returned result if the query match at least 5 fields.
I hope i was clear, thank you.
SELECT *
FROM `someTable`
WHERE (`field_A` LIKE '%wordToFind%'
OR `field_B` LIKE '%wordToFind%')
AND (SELECT COUNT(*)
FROM `someTable`
WHERE (`field_A` LIKE '%wordToFind%'
OR `field_B` LIKE '%wordToFind%')) >= 5
This might not be very efficient, as it may search for the rows twice, first for the subquery and then for the main query.

MySQL / PHP query performance?

Im in a dilema on which one of these methods are most efficient.
Suppose you have a query joining multiple tables and querying thousand of records. Than, you gotta get the total to paginate throughout all these results.
Is it faster to?
1) Do a complete select (suppose you have to select 50's columns), count the rows and than run another query with limits? (Will the MySQL cache help this case already selecting all the columns you need on the first query used to count?)
2) First do the query using COUNT function and than do the query to select the results you need.
3) Instead of using MySQL COUNT function, do the query selecting the ID's for example and use the PHP function mysql_num_rows?
I think the number 2 is the best option, using MySQL built in COUNT function, but I know MySQL uses cache, so, selecting all the results on first query gonna be faster?
Thanks,
Have a look at Found_Rows()
A SELECT statement may include a LIMIT clause to restrict the number
of rows the server returns to the client. In some cases, it is
desirable to know how many rows the statement would have returned
without the LIMIT, but without running the statement again. To obtain
this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT
statement, and then invoke FOUND_ROWS() afterward:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
The second SELECT returns a number indicating how many rows the first SELECT`
would have returned had it been written without the LIMIT clause.
My guess is number 2, but the truth is that it will depend entirely on data size, tables, indexing, MySql version etc.
The only way of finding the answer to this is to try each one and measure how long they take. But like I say, my hunch would be number 2.