This query on mysql is taking forever to execute - php

im making a simple admin module to query the database to show the results. Im using this query via php:
SELECT
*
FROM myTable
WHERE id in(SELECT
id_registro
FROM myOtherTable
where id_forma='".$id_club."' and fecha_visita Like '%".$hoy."%'
)
order by id DESC
The result shows, however, it takes very long like 2 minutes..Anyone can help me out?
Thanks!

Without seeing your database, it is hard to find a way to make it faster.
Maybe you can try to turn your WHERE IN to INNER JOIN. To something like this
SELECT * FROM myTable INNER JOIN myOtherTable
ON (myTable.id = myOtherTable.id_registro)
WHERE myOtherTable.id_forma = '$id_club'
AND myOtherTable.fecha_visita LIKE '%$hoy%'
ORDER BY myTable.id DESC
Noted that you should sanitize your variable before putting it SQL query or using PDO prepare statement.

Sub Queries takes always time, so its better to ignore them as much as possible.
Try to optimize your query by checking its cardinality,possible keys getting implemented by DESC or EXPLAIN , and if necessary use FORCE INDEX over possible keys.
and I guess you can modify your query as:
SELECT
*
FROM myTable
inner join id_registro
on (id = id_forma )
where
id_forma='".$id_club."' and fecha_visita Like '%".$hoy."%'
order by id DESC

LIKE in mysql may take a long time ,with or without index.
Do u have a very large DB?

Related

How to order the ORDER BY using the IN() mysql? [duplicate]

I am wondering if there is away (possibly a better way) to order by the order of the values in an IN() clause.
The problem is that I have 2 queries, one that gets all of the IDs and the second that retrieves all the information. The first creates the order of the IDs which I want the second to order by. The IDs are put in an IN() clause in the correct order.
So it'd be something like (extremely simplified):
SELECT id FROM table1 WHERE ... ORDER BY display_order, name
SELECT name, description, ... WHERE id IN ([id's from first])
The issue is that the second query does not return the results in the same order that the IDs are put into the IN() clause.
One solution I have found is to put all of the IDs into a temp table with an auto incrementing field which is then joined into the second query.
Is there a better option?
Note: As the first query is run "by the user" and the second is run in a background process, there is no way to combine the 2 into 1 query using sub queries.
I am using MySQL, but I'm thinking it might be useful to have it noted what options there are for other DBs as well.
Use MySQL's FIELD() function:
SELECT name, description, ...
FROM ...
WHERE id IN([ids, any order])
ORDER BY FIELD(id, [ids in order])
FIELD() will return the index of the first parameter that is equal to the first parameter (other than the first parameter itself).
FIELD('a', 'a', 'b', 'c')
will return 1
FIELD('a', 'c', 'b', 'a')
will return 3
This will do exactly what you want if you paste the ids into the IN() clause and the FIELD() function in the same order.
See following how to get sorted data.
SELECT ...
FROM ...
WHERE zip IN (91709,92886,92807,...,91356)
AND user.status=1
ORDER
BY provider.package_id DESC
, FIELD(zip,91709,92886,92807,...,91356)
LIMIT 10
Two solutions that spring to mind:
order by case id when 123 then 1 when 456 then 2 else null end asc
order by instr(','||id||',',',123,456,') asc
(instr() is from Oracle; maybe you have locate() or charindex() or something like that)
If you want to do arbitrary sorting on a query using values inputted by the query in MS SQL Server 2008+, it can be done by creating a table on the fly and doing a join like so (using nomenclature from OP).
SELECT table1.name, table1.description ...
FROM (VALUES (id1,1), (id2,2), (id3,3) ...) AS orderTbl(orderKey, orderIdx)
LEFT JOIN table1 ON orderTbl.orderKey=table1.id
ORDER BY orderTbl.orderIdx
If you replace the VALUES statement with something else that does the same thing, but in ANSI SQL, then this should work on any SQL database.
Note:
The second column in the created table (orderTbl.orderIdx) is necessary when querying record sets larger than 100 or so. I originally didn't have an orderIdx column, but found that with result sets larger than 100 I had to explicitly sort by that column; in SQL Server Express 2014 anyways.
SELECT ORDER_NO, DELIVERY_ADDRESS
from IFSAPP.PURCHASE_ORDER_TAB
where ORDER_NO in ('52000077','52000079','52000167','52000297','52000204','52000409','52000126')
ORDER BY instr('52000077,52000079,52000167,52000297,52000204,52000409,52000126',ORDER_NO)
worked really great
Ans to get sorted data.
SELECT ...
FROM ...
ORDER BY FIELD(user_id,5,3,2,...,50) LIMIT 10
The IN clause describes a set of values, and sets do not have order.
Your solution with a join and then ordering on the display_order column is the most nearly correct solution; anything else is probably a DBMS-specific hack (or is doing some stuff with the OLAP functions in standard SQL). Certainly, the join is the most nearly portable solution (though generating the data with the display_order values may be problematic). Note that you may need to select the ordering columns; that used to be a requirement in standard SQL, though I believe it was relaxed as a rule a while ago (maybe as long ago as SQL-92).
Use MySQL FIND_IN_SET function:
SELECT *
FROM table_name
WHERE id IN (..,..,..,..)
ORDER BY FIND_IN_SET (coloumn_name, .., .., ..);
For Oracle, John's solution using instr() function works. Here's slightly different solution that worked -
SELECT id
FROM table1
WHERE id IN (1, 20, 45, 60)
ORDER BY instr('1, 20, 45, 60', id)
I just tried to do this is MS SQL Server where we do not have FIELD():
SELECT table1.id
...
INNER JOIN
(VALUES (10,1),(3,2),(4,3),(5,4),(7,5),(8,6),(9,7),(2,8),(6,9),(5,10)
) AS X(id,sortorder)
ON X.id = table1.id
ORDER BY X.sortorder
Note that I am allowing duplication too.
Give this a shot:
SELECT name, description, ...
WHERE id IN
(SELECT id FROM table1 WHERE...)
ORDER BY
(SELECT display_order FROM table1 WHERE...),
(SELECT name FROM table1 WHERE...)
The WHEREs will probably take a little tweaking to get the correlated subqueries working properly, but the basic principle should be sound.
My first thought was to write a single query, but you said that was not possible because one is run by the user and the other is run in the background. How are you storing the list of ids to pass from the user to the background process? Why not put them in a temporary table with a column to signify the order.
So how about this:
The user interface bit runs and inserts values into a new table you create. It would insert the id, position and some sort of job number identifier)
The job number is passed to the background process (instead of all the ids)
The background process does a select from the table in step 1 and you join in to get the other information that you require. It uses the job number in the WHERE clause and orders by the position column.
The background process, when finished, deletes from the table based on the job identifier.
I think you should manage to store your data in a way that you will simply do a join and it will be perfect, so no hacks and complicated things going on.
I have for instance a "Recently played" list of track ids, on SQLite i simply do:
SELECT * FROM recently NATURAL JOIN tracks;

sql updating only one row

I am trying to update only one row using sql, but I am having troubles with it.
I am trying to do something like this:
$sql="UPDATE table SET age='$age' WHERE id=(SELECT id FROM another_table WHERE somecondition ORDER BY id LIMIT 1)";
but this is not updating anything. I feel like there is some error with where the parenthesis are, but I am not sure what exactly is wrong with it. Does anybody have any idea? or have other suggestions on how to update only one row that satisfies the given conditions?
Edited Notes:
Okay, I may have made my question too complicated. Let me rephrase my question; What is the generic way of updating only 1 row that meets certain conditions. It can be any row if the row meets the conditions.
you should run this query firstly:
SELECT id FROM another_table WHERE somecondition ORDER BY id LIMIT 1
and see the result, if you get specific value, say for example 1 , update your code to be
$sql="UPDATE table SET age='$age' WHERE id=(1)";
and you can see the results. if the query doesn't produce errors so your condition doesn't consider and there is no 1 id in your table table.
I have found that updating based on a condition in a sub-query, as in your example, sometimes has problems that seem due to the database trying to figure out the best execution path. I have found it better to do something like the following, noting that my code is in T-SQL and may need a smidgen of tweaking to work in MySQL.
UPDATE T1 SET age=#Age
FROM table as T1 INNER JOIN
another_table as T2 ON T1.id = T2.id
WHERE [use appropriate conditions here]
Try running this query:
UPDATE table t
SET t.age='$age'
WHERE t.id = (SELECT a.id
FROM another_table a
WHERE somecondition
ORDER BY a.id
LIMIT 1
);
One not-uncommon cause of this error is when the id column has different names. You should get in the habit of qualifying column names. You should also verify that the ids in the two tables are intended to match.
Another cause would simply be that the matched conditions return no row or ids that are not in the table. That is a bit harder to fix, which better understanding the data and data structure.

Unions in mysqli

I am attempting to get the latest result from two different tables (forum_posts and forum_replies)...I'm not sure of the best way of doing this, so I am attempting to use a UNION ALL to do this...
I did a test result to try to make sure the code worked; however, it doesn't appear to be working correctly. Even though there is data matching the requirements in the database, it is echoing out No Posts. So something about the query isn't processing correctly.
$latest = "(SELECT * FROM forum_posts WHERE post_subcat = '1' ORDER BY post_id) UNION ALL (SELECT * FROM forum_replies WHERE reply_subcat = '1' ORDER BY reply_id) LIMIT 1";
if(!$getlatest = $con->query($latest)){ echo 'No Posts'; }
if($getlatest = $con->query($latest)){ echo 'Post'; }
I'm new to unions, so I have a few questions.
1) I've seen a union work in mysql, but do they also work in mysqli?
2) Are there any restrictions to using unions (union/union all/ etc.)? Do columns have to be the same for comparison?
3) Did I do something wrong in my above code? I am probably overlooking something minor just from working too long, just not sure at this point.
You generally have to (overly flexible DBMS' notwithstanding) have the same column types and identifiers in the two queries you're unioning together and the order by applies to the final result set, not the interim one (although you can use sub-queries to order interim results if needed, not something that seems to be required in this specific case).
So something like this:
SELECT post_id as id,
post_date as dt
FROM forum_posts
WHERE post_subcat = '1'
UNION ALL
SELECT reply_id as id,
reply_date as dt
FROM forum_replies
WHERE reply_subcat = '1'
ORDER BY dt DESC
LIMIT 1
In terms of the mysql/mysqli distinction, that shouldn't matter, both methods end up doing the relevant work at the server side.
You usually use union all if you know there's no chance of duplicates so as to avoid any unnecessary sorting for the duplicate removal. Otherwise, use union if you do want duplicates removed.
As to whether you have a problem with your code, if the query function returns false, you should be checking the error functions for the specific failure reason. See mysqli_error for details.
And keep in mind it doesn't return false if there were no posts, it only returns false if there was an error. If the query worked and there were no posts, you'd end up with an empty result set.
1) I've seen a union work in mysql, but do they also work in mysqli?
It should work on mysqli also because mysqli is another extension to access Mysql 4.1 onwards
2) Are there any restrictions to using unions (union/union all/ etc.)? Do columns have to be the same for comparison?
Yes. All the columns from two tables needs to be of same data type and the no of columns also should match. I suspect this is your problem.
3) Did I do something wrong in my above code? I am probably overlooking something minor just from working too long, just not sure at this point.
If I've understood what you're after correctly, I believe the following will work:
SELECT * FROM forum_posts fp WHERE fp.post_subcat = (SELECT MAX(fp1.post_subcat) FROM forum_posts fp1)
UNION
SELECT * FROM forum_replies fr WHERE fr.reply_subcat = (SELECT MAX(fp.post_subcat) FROM forum_posts fp)
Did I do something wrong in my above code?
Sure.
First, you are running your query two times.
Second, you are checking wrong value to see if there were any posts. http://php.net/mysqli_query

MySQL UNION SELECT until found?

Let's say I have table with column 'URL' whrere I store urls like this
one/two
one/two/three
alpha/omega
And I want to get data from database for specific url and if it is not found I remove the last part of url and search again:
Example:
I have url like one/two/three/four/five.
I do search for "one/two/three/four/five"
if not found search again for "one/two/three/four"
if not found search again for "one/two/three"
if not found search again for "one/two"
I would like to have something like:
SELECT * FROM db WHERE url=one/two/three/four/five
UNION
SELECT * FROM db WHERE url=one/two/three/four/five
UNION
SELECT * FROM db WHERE url=one/two/three/four
UNION
SELECT * FROM db WHERE url=one/two/three
UNION
SELECT * FROM db WHERE url=one/two
UNION
SELECT * FROM db WHERE url=one
but I want to stop searching if the row is found.
Is this possible or do I have to do it with separated queries.
Thanks for help.
I thing that this is the most elegant approach to your question. This statement is independent depth path and you don't need to split constant url in subsequent selects:
SELECT
*
FROM
db
WHERE
concat( 'one/two/three/four/five' , '/') like concat( url , '/%')
ORDER BY
LENGTH (url) desc
LIMIT 1
I have tested this query in MySQL, also you can check it! (in MSSQL syntax)
Just replace UNION with UNION ALL and add LIMIT 1 at the end.
P.S. UNION ALL would not make much difference in this particular example, but it is useful to know the difference: http://www.mysqlperformanceblog.com/2007/10/05/union-vs-union-all-performance/
Let's say I have table with column 'URL' whrere I store urls like this
one/two
one/two/three
alpha/omega
Don't do that, it's a horrible design and the proof is the problem you are having running such a simple query; store each URl on a DIFFERENT ROW. Read up on Normalization.
You could use a regexp to search in a single query, but it'll get you all rows:
SELECT * FROM db WHERE url REGEXP "^one((/two)|(/two/three)|(two/three/four)|(/two/three/four/five))?$"
so if you only want the results from the first WHERE you'll have to do multiple queries.
If you really want to have a single query and don't care about a little overhead in the search you could do
SELECT * FROM db WHERE url REGEXP "^one((/two)|(/two/three)|(two/three/four)|(/two/three/four/five))?$" ORDER BY length(url) DESC LIMIT 1
This will get you the first possible result only, but the query inside will have to get all possible results first -> less efficient, but more compact.
I hope this helps!

MySQL select between two dates not working as expected

I'm trying to create a query that will select all dates between two dates
This is my query:
$query = "SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '$date1' AND '$date2' AND D1.D1_ID = D2.D2_ID";
The trouble is, it is not returning anything, but not producing an error either
So I tried inputting it directly into phpMyAdmin like this
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
AND D1.D1_ID = D2.D2_ID`
then like this
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
and like this
SELECT * FROM D1
WHERE DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
and I just get
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0003 sec )
Yes, my tables exist, and so do the columns :)
In the first cases the lack of results could be because of the inner join. For a result to be in the set it would require a record in both tables, ie. a record from d1 would not appear unless d2 also had that id in the d2_id column. To resolve this, if that is correct for your business logic, use left join.
However, the last of your cases (without the join) suggests the reasons is a lack of matching records in the first (left) table d1.
Without the full dataset we can't really comment further, since all the code you are running is perfectly valid.
If you always want to select an entire year it is easer to select it like this:
SELECT * FROM D1 WHERE YEAR(DATE_ADDED) = 2011;
Please implement below code
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN DATE_FORMAT('2011-01-01','%Y-%m-%d')
AND DATE_FORMAT('2011-12-12','%Y-%m-%d')
AND D1.D1_ID = D2.D2_ID`

Categories