Is it possible to optimize this where clause? [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Can anyone help me with this? I have doubts about the below function; can I create a virtual column for this?
select as1.col,as1.col2,as1.col3 from
analytics.adjusted_sale_velocity
where
date(as1.created_datetime)=(
select
max(
date(created_datetime)
)
from
analytics.adjusted_sale_velocity
)

MySQL optimizer won't use an index once a column in the WHERE clause is wrapped with a function, date in your case.
Your query might be written a little different:
select as1.col,
as1.col2,
as1.col3
from adjusted_sale_velocity a
inner join ( select max(created_datetime) as created_datetime
from adjusted_sale_velocity
) as max_dt on left(a.created_datetime,10) = left(max_dt.created_datetime,10) ;
Try and let me know if it is faster.

Related

How to improve this select statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I wrote this SQL
select *
from t_info
where contract_id <> "" and status <> "unfinished"
order by id asc;
but it takes more than 4s. I wonder how to improve this SQL? Thank.
Please don't use "<>" in where condition, while you need to use index.
select * from t_info
where LENGTH(trim(contract_id)) > 0
and (not status = "unfinished")
order by id asc;

update with select result conditional takes too long [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
update A
set step1_response='step2',
step1_editor='step2'
where ch_idx in (select B.ch_idx
from B
where B.check_e_done_ct=B.check_e_tot_ct
AND B.check_w_tot_ct=B.check_w_done_ct);
The result of select inside the parentheses is about 4000 rows.
The table to be updated (A) is about 90000 rows.
the command above takes forever to complete.
how can i re-write this query to get result faster?
thank you!
UPDATE a
JOIN b USING (ch_idx)
SET a.step1_response='step2',
a.step1_editor='step2'
WHERE b.check_e_done_ct = b.check_e_tot_ct
AND b.check_w_tot_ct = b.check_w_done_ct;
or maybe
UPDATE a
JOIN ( SELECT ch_idx
FROM b
WHERE b.check_e_done_ct = b.check_e_tot_ct
AND b.check_w_tot_ct = b.check_w_done_ct ) bb USING (ch_idx)
SET a.step1_response='step2',
a.step1_editor='step2';

Duplicate value while using Group By [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
SELECT location,special,price FROM `tickets`
WHERE event = 'food' GROUP BY location
I think you want, (assuming its mysql) the following:
SELECT location, ANY_VALUE(special) special, ANY_VALUE(price) price
FROM tickets
WHERE event='food'
GROUP BY location ;
ANY_VALUE is strange aggregate function out of normal db standards that returns one of the values arbitarily.
If you wish to not have duplicate results in your rows you can choose only distinct result set.
SELECT DISTINCT location,special,price FROM tickets WHERE event = 'food' GROUP BY location

String retrieve Function in SQL Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am having a database with 2 columns employee and job.Job column contains character in each record.In that,I have to list the employees whose job is having last 3 characters as 'man'.I have to use only functions in SQL Query.
Use LIKE with wildcard:
SELECT *
FROM your_table
WHERE job_column_name LIKE '%man'
or using RIGHT
SELECT *
FROM your_table
WHERE RIGHT(job_column_name, 3) = 'man'
Answer For Question 4 is
SELECT *
FROM emp
WHERE SUBSTR(job,-3,3)='MAN';

MySql: how to get unique id from two field in a table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There is a table called member_info and this table have 2 rows (member_id and sponsor_id)
table looks like :
member_id (1,2,3,4,5,6,7,8);
sponsor_id (,1,1,1,2,5,5,5);
Now i need to pick those member_id's who are not in sponsor_id like
member_id(3,4,6,7,8);
Any help Appreciated.
You can use the NOT IN function with a sub-query:
SELECT `member_id`
FROM `member_info`
WHERE `member_id` NOT IN (
SELECT DISTINCT(`member_id`) FROM `sponsor_info`
)
Subqueries introduced with the keyword NOT IN also return a list of zero or more values.
SELECT member_id
FROM member_inf
WHERE member_id NOT IN (
SELECT DISTINCT(member_id) FROM sponsor_info
)