Negative INNER JOIN [Without LEFT JOIN & IS NULL] - mysql

I have 2 tables with tth hashes, and i need to get which of them, who exist in first table but not exist in second table.
I'll try something like this:
SELECT f.*
FROM files as f
LEFT JOIN trans as t ON t.tth=f.tth
WHERE t.id IS NULL
But it's working very slow, in first table 65k lines, and second table with 130k lines, so query working for ~5 minutes.
Here exist another way?
Thanks.
P.S. All columns in both tables having indexes.

Thanks, OMG Ponies, I read an article on the link, and a little optimizing method using NOT IN, query rate was ~ 0.6 seconds.
SELECT f.*
FROM files as f
WHERE
f.tth NOT IN (
SELECT trans.tth
FROM trans as t
-- We do not need to look entries across entire table, only from already matched, so create selection "limits" with INNER JOIN.
INNER JOIN files ON files.tth=t.tth
)

Related

Join clause from one table to another to where one table has its foreign key mixed

So, I wish to join two tables (reservation and facility_items). The reservation.facility_item_id is composed of multiple facility_item_id in a single row. And now I am having trouble joining them because I can't connect the reservation.facility_item_id to facility_items.facility_item_id
Here is the structure of my tables:
reservation
facility_item
reservation content
facility_item content
`SELECT * FROM reservations r
JOIN facility_items f on r.facility_item_id IN
(
SELECT facility_item_id
FROM facility_items
)`
lmao. I do not even know what I am doing but thats my start.
Expected result should show 3 facility_item_id but instead, I got this:
Wow, I really didn't check the content on the tables, it is indeed more complicated than a simple join. I guess you are complicating things because this a 1xN relation and you are storing it on the wrong side (the 1 side instead of the N side).
You should have a field in your facility table which shoud store the ID of the reservation. Then a simple join will suffice:
SELECT *
FROM reservations r JOIN facility f
ON r.reservation_id = f.reservation_id
First attempt, Won't work
It seems that a simple join should suffice. Can you try this one?
SELECT *
FROM reservations r JOIN facility_items f
ON r.facility_item_id = f.facility_item_id

MySQL WHERE NOT EXISTS not working

I have two tables, one called st_grid that has soccer matches in it, and another table, st_compiled, that is essentially a copy of st_grid but the process of putting a row in st_compiled is quite intensive so I want to put the data in one row at a time. The two tables are like this with the pertinent columns:
st_grid
id
league_id
fixture_date
inplay_fixture_compiled
inplay_fixture_compiled_id
grid_id
I want to select a row from st_grid where there is no corresponding row in st_compiled ON grid_id, but I havent had any luck. I have looked up various queries and am trying this one
SELECT g.id
FROM st_grid g
WHERE NOT EXISTS
(SELECT i.grid_id
FROM inplay_fixture_compiled i
WHERE g.id = i.grid_id)
AND g.league_id = '15'
But it isnt working, all that is happening is the page is hanging for minutes when I try and run it. There is about 170,000 rows in st_grid (but for each league_id there will max 600 rows) and 10,000 in st_compiled but I dont believe that that is a huge amount by any means.
Hope that makes sense, any help much appreciated.
P
Why don't you try join in the case to get all the data in st_grid that is not yet exists in st_compiled.
For e.g;
select grid_id
from st_grid t1
left join st_compiled t2 on t1.grid_id = st_compiled.grid_id
where t2.grid_id is null
SELECT g.grid_id
FROM st_grid g
LEFT JOIN inplay_fixture_compiled i ON (g.id = i.grid_id)
WHERE g.league_id = '15' and i.grid_id is null.
You can join these tables with a LEFT JOIN keyword and filter out the NULL's, but this will likely be less efficient than using NOT EXISTS.

How can i execute this query faster?

This is my query:
create table vi_all as
select
d.primaryid, d.age, d.gndr_cod, d.wt, d.wt_cod, d.reporter_country,
dr.primaryiddrug, dr.role_cod, dr.drug_name,
r.primaryidreac, r.pt,
o.primaryidoutc, o.outc_cod,
i.primaryidindi, i.indi_pt
FROM demo d,
drug dr,
reac r,
outc o,
indi i;
Each table contains at least 80K records and more than 20 fields so its getting really tough to execute select statement on multiple tables; and i just want 4 or 3 fields from each table so i thought of this, but the above query has taken more than 5 hours but still has not given back any result.
My crystal ball says you need something like this:
create table vi_all as
select
d.primaryid, d.age, d.gndr_cod, d.wt, d.wt_cod, d.reporter_country,
dr.primaryiddrug, dr.role_cod, dr.drug_name,
r.primaryidreac, r.pt,
o.primaryidoutc, o.outc_cod,
i.primaryidindi, i.indi_pt
FROM demo d
LEFT JOIN drug dr ON d.drug=dr.id
LEFT JOIN reac r ON d.reac=r.id
LEFT JOIN outc o ON d.outc=o.id
LEFT JOIN indi i ON d.indi=i.id;
As far as I can tell your query is selecting all results from all tables but not assocating them in anyways so, you can maybe get duplicate data in the newly created table. Also, if you have some good foreign keys to associate those tables, the performance will be considerably better.

MySQL query for finding rows that are in one table but not another

Let's say I have about 25,000 records in two tables and the data in each should be the same. If I need to find any rows that are in table A but NOT in table B, what's the most efficient way to do this.
We've tried it as a subquery of one table and a NOT IN the result but this runs for over 10 minutes and almost crashes our site.
There must be a better way. Maybe a JOIN?
Hope LEFT OUTER JOIN will do the job
select t1.similar_ID
, case when t2.similar_ID is not null then 1 else 0 end as row_exists
from table1 t1
left outer join (select distinct similar_ID from table2) t2
on t1.similar_ID = t2.similar_ID // your WHERE goes here
I would suggest you read the following blog post, which goes into great detail on this question:
Which method is best to select values present in one table but missing
in another one?
And after a thorough analysis, arrives at the following conclusion:
However, these three methods [NOT IN, NOT EXISTS, LEFT JOIN]
generate three different plans which are executed by three different
pieces of code. The code that executes EXISTS predicate is about 30%
less efficient than those that execute index_subquery and LEFT JOIN
optimized to use Not exists method.
That’s why the best way to search for missing values in MySQL is using a LEFT JOIN / IS NULL or NOT IN rather than NOT
EXISTS.
If the performance you're seeing with NOT IN is not satisfactory, you won't improve this performance by switching to a LEFT JOIN / IS NULL or NOT EXISTS, and instead you'll need to take a different route to optimizing this query, such as adding indexes.
Use exixts and not exists function instead
Select * from A where not exists(select * from B);
Left join. From the mysql documentation
If there is no matching row for the right table in the ON or USING
part in a LEFT JOIN, a row with all columns set to NULL is used for
the right table. You can use this fact to find rows in a table that
have no counterpart in another table:
SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id =
right_tbl.id WHERE right_tbl.id IS NULL;
This example finds all rows in left_tbl with an id value that is not
present in right_tbl (that is, all rows in left_tbl with no
corresponding row in right_tbl).

MySQL Join with many (fields) to one (secondary table) relationship

I have a query I need to perform on a table that is roughly 1M records. I am trying to reduce the churn, but unfortunately there is a UNION involved (after i figure this join out), so that may be a question for another day.
The records and data I need to get reference 3 fields in a table that need each pull a description from another table and return it in the same record, but when i do the Inner join i was thinking, it either returns only 1 field fromt he other table, or multiple records from he original table.
Here are some screen shots of the tables and their relationship:
Primary table containing records (1 each) with the physician record I want to pull, including up to 3 codes that can be listed in the "taxonomy" table.
Secondary table containing records (1 each) with the "Practice" field I want to pull.
A Quick glance of the relationship i'm talking about
I presume that if perform an inner join matching the 3 fields in the physicians table, that it will have to iterate that table multiple times to pull each taxonomy code .. but I still can't even figure the syntax to easily pull all of these codes instead of just 1 of them.
i've tried this:
SELECT
taxonomy_codes.specialization,
physicians.provider_last_name,
physicians.provider_first_name,
physicians.provider_dba_name,
physicians.legal_biz_name,
physicians.biz_practice_city
FROM
taxonomy_codes
INNER JOIN physicians ON physicians.provider_taxonomy_code_1 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_2 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_3 = taxonomy_codes.taxonomy_codes
First, the query churns a lot and it only returns one taxonomy specialty result which I presume is because of the OR in the join statement. Any help would be greatly appreciated.
Thank you,
Silver Tiger
You have to join the taxonomy_codes table multiple times:
SELECT p.provider_last_name, p...., t1.specialization as specialization1, t2.specialization as specialization2, t3.specialization as specialization3
FROM physicians p
LEFT JOIN taxonomy_codes t1 ON t1.taxonomy_codes = provider_taxonomy_code_1
LEFT JOIN taxonomy_codes t2 ON t2.taxonomy_codes = provider_taxonomy_code_2
LEFT JOIN taxonomy_codes t3 ON t3.taxonomy_codes = provider_taxonomy_code_3