Why Does This MySQL Query Not Work On The Live Server - mysql

I have a query that works when I troubleshoot this within my code on my local pc, it works perfectly but the moment I put this up on the live server the query fails for some reason, I think, but I'm not sure, that this boils down to the date fields somehow but honestly I have no idea why it's failing on the server. I have tried the sql functions Date, date_to_string and my dates are in the format '2019-03-08' please assist, my query is as per below.
select posted_ads.id, posted_ads.title,
posted_ads.description, posted_ads.price,
posted_ads.datePosted,
posted_ads.adCategory,
posted_ads.adSuspended, posted_ads.suspensionEmailSent,
posted_ads.adSuspensionReason, posted_ads_images.id, posted_ads_images.imageName,
posted_ads_images.type, posted_ads_images.adID
from posted_ads inner join
posted_ads_images
on posted_ads.id = posted_ads_images.adID
where title like '%mechanic%' and
posted_ads.adExpired = 0 and
posted_ads.datePosted between '2018-12-09' and '2019-03-09'
group by posted_ads.id
limit 6 offset 6

Related

phpMyadmin Query Execution Time

I tried to run the following query in my Cloud VPS cPanel phpMyadmin
SELECT bankcode, bankname
FROM newbankdetails
WHERE
type='DB' AND
bankcode IN ( SELECT drawingbankname
FROM dd1
WHERE entrydate BETWEEN '01/04/2014' AND '30/04/2014'
AND paymentmode='DD'
AND state='TAMIL NADU' )
It takes very very very long time nearly 5 to 6 hours and says out of time or keeps running for days and don't show any error also don't show results.
Whereas it works perfectly in my local machine xampp
It happens only when working on large size tables like 12GB and around like that
How to speed it up and display the result as it displays instantly in localhost xampp
I think, replacing IN with a LEFT JOIN can make more sense, like this:
SELECT
bankcode, bankname
FROM
newbankdetails nd
LEFT JOIN
dd1 ON nd.bankcode = dd1.drawingbankname
WHERE
nd.type='DB'
AND
dd1.entrydate BETWEEN '01/04/2014' AND '30/04/2014'
AND
dd1.paymentmode = 'DD'
AND
dd1.state = 'TAMIL NADU'
I can't test it, But I think with dd1.paymentmode = 'DD' automatically removes rows of dd1.paymentmode IS NULL because of using LEFT JOIN.
First you should use EXPLAIN QUERY (https://dev.mysql.com/doc/refman/5.0/en/using-explain.html)
That will give you information about what is slow.
Your problem seems to be related to the size of the table. On your localhost you may have a small table, but in production it is very large. Also put an index on the first table on nd.bankcode.
You should try to remove the subselect, and use a JOIN instead.
Also, you should put indexes on the columns you use for search.
Put an index on drawingbankname, entrydate, paymentmode, state. Put also an index on nd.backcode .
Column drawingbankname is varchar, you should convert it to fixed char.
Remove the subselect and use JOIN.
Use EXPLAIN after the changes to see progress.

Mysql: Query not working on another PC with the same config

I have a query running fine on my current PC that is my developpement PC but not in production environment.
The two environement have the same material configuration.
The setting of the database are the same two.
On dev the query execute in less than 30 sec, but when I try it on production even after 2 hours I don't get result.
This is the only running query on the server at the moment of execution.
Do you have any idea of why this is happening ?
SELECT td.td_date,
td.td_number,
td.td_status,
td.td_open_datetime,
td.td_update_datetime,
Min(ack.ackup) ackUpdate
FROM t_ticketdossier_td td
INNER JOIN (SELECT a.td_number,
a.td_update_datetime ackUp
FROM t_ticketdossier_td a
WHERE a.td_status = 'Acknowledged') ack
ON td.td_number = ack.td_number
AND td.td_update_datetime <= ack.ackup
WHERE td.td_number IN (SELECT td_number
FROM t_ticketdossier_td base
WHERE Date(base.td_date) = #date
AND base.td_status = 'Acknowledged'
AND base.td_scope IS NULL
AND base.td_subcategory NOT LIKE '%RDV%'
AND base.td_product_type NOT LIKE '%RDV%'
GROUP BY td_number)
AND td.td_status = 'Affected'
GROUP BY td.td_date,
td.td_number,
td.td_status,
td.td_update_datetime
Explain from the dev server.
Explain from the prod server.
Sorry for the links, I don't have enougth reputation to post the image directly.
Cheers,
Maxime.
From what I see in the EXPLAIN statements, it seems that your production database misses quite a few indexes.
For example:
TD_STATUS_IDX,TD_NUMBER_IDX
Try adding these and run the EXPLAIN again.

Differences between SQL Fiddle and local

Over here: MySQL: Get start & end timestamp for each day I was given a working answer, but it only works for me in SQL Fiddle (http://sqlfiddle.com/#!2/62366/1). When I run the same SQL on my Debian Linux / MySQL 5.5.34 I get incorrect results (start & end times are the closest reading to midnight)! Anyone got any suggestions as to what might be causing the problem?
My mistake - real schema had another column for deviceID, and the data held readings for multiple devices. Once I added the following, all worked OK:
...
FROM myTable
HAVING deviceID = 1234
GROUP BY deviceID, StatusTime

Why would "WHERE `DATE` >={ts '2013-12-01 00:00:00'}" produce a different result to "WHERE `DATE` >='2013-12-01 00:00:00'"?

As title says - why would the first query (generated automatically by Crystal Reports) above produce a different result (unreliably - the first query gives different results depending on when I run it - the database is not being modified) to the second query above? Going by this reference guide: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-literals.html, the output should be the same.
Can anyone offer any advice? Note I am using MySQL version 5.6.15, and this problem only started to occur when we upgraded

Strange Mysql error 1111, supposedly worked before

I am trying to troubleshoot a particular 'report' that gets generated on a PHP application running on a VOIP platform (billing related). The person that asked me to do this stated "it worked before" ;)
MySQL Error thrown:
1111: Invalid use of group function
Crazy thing is, it 'worked before' but stopped after awhile, they cannot place what event took place that might have created this bug. One thought is maybe MYSQL was upgraded? I googled the error, and can't seem to find a clear answer.
At first glance do you guys see anything wrong here in the SQL?
SELECT C.ResourceGroupID AS CustomerID, CS.CustomerName, SUM(C.IN_RndDuration/60) AS Minutes, SUM(CASE WHEN (C.OUT_Duration>0 AND C.IN_RndDuration>0) THEN 1 ELSE 0 END) AS Successfull, count(0) AS Attempts
FROM ws_call_current AS C
LEFT JOIN customer_rg AS V_RG ON
V_RG.RGId=C.OtherResourceGroupID AND V_RG.RateTableId IS NOT NULL AND V_RG.Direction='O'
LEFT JOIN customer AS V ON V.CustomerId=V_RG.CustomerId
LEFT JOIN customer_rg AS CS_RG ON CS_RG.RGId=C.ResourceGroupID AND CS_RG.RateTableId IS NOT NULL AND CS_RG.Direction='I'
LEFT JOIN customer AS CS ON CS.CustomerId=CS_RG.CustomerId
WHERE DATE_FORMAT(DATE_ADD(C.SeizeDate, INTERVAL TIME_TO_SEC(C.SeizeTime) SECOND), '%Y-%m-%d %H:%i:00') BETWEEN '2010-10-19 00:00:00' AND '2010-10-19 23:59:59'
AND C.SeizeDate BETWEEN '2010-10-19' AND '2010-10-19'
GROUP BY CS.CustomerName
ORDER BY SUM(C.IN_RndDuration/60) DESC
This is written for MYSQL 3/4 I believe, not yet sure. Just wanted to get some feedback.
From google results I find some people had success with fixing this by modifying the query to be a Having instead of a where?
Not sure. Anything look odd below?
Try including C.ResourceGroupID in your group by statement. You need to include all non-aggregate items in your select in your group by.
You shouldn't need a having in the code above.
I'm not sure how it could have worked before...
What I found after review is that the cause of this was simply a MySQL discrepancy. Initially the MySQL code was written for 5.0.51a, and the running MySQL version (it was somehow 'attempted' to be upgraded, was 5.0.21).
This version 'downgrade' was the cause of the failed SQL. I honestly don't know what the cause was other than we installed a new version of MySQL to just do a test (on a remote box) and it solved the issue (pointing from primary box to backup MySQL server.
Solution for this error: Version compatibility