I have encountered with a problem while joining table with a view
select count(*) from table A inner join table B
on A.scen_id=B.scen_id
and A.value=10 --(till here records returned are 0)
inner join View C --this view contains millions of records
on B.scen_id=C.scen_id
Since join of two tables A and B returns 0 records but the whole query is takinh ~ 2 Hrs just because of View and gives me 0 records.
Is there any solution to this problem so i can get 0 records with a good performance?.
Can you try following, send explain and lets know if it is working or not:
select count(*)
from table A
inner join table B
on A.scen_id=B.scen_id
inner join View C
on B.scen_id=C.scen_id
where A.value=10
also make sure stats on A.Value have been refreshed.
Related
I'm new to MS Access and database concepts. I have a linked table with 9500 records and another table with 72 records. In a query I used LEFT JOIN to combine both table which has common ID's. I did this to do DSUM. After joining my MS- ACCESS 2010 hangs up and loads very very slow. Here is my code for left join
SELECT column1, column2
FROM Table1 LEFT JOIN Table2 ON Table1.ID = Table2.ID.
Can anyone help me to optimize this left outer join in MS ACCESS 2010.
Thanks for your help.
I need to build a query that I believe needs different types of joins, but I am not sure how to write it.
I have a customers table and an orders table. I need a query that will return 2 columns displaying the following:
1- list of all customer_id that have never had an order
2- list of all customer_ID that have 1 or more record in the orders table
I have this left join query but i feel like its missing something that indicates the customers column should not have a value on the orders table?
SELECT Customers.userid, Orders.userid
FROM Customers
LEFT JOIN ORDERS
ON Customers.userid=orders.USER_ID
I have 4 tables, project, project_status, invoice and purchase.
I have the following query I have created, jut can't get my head around the type of joins with more than 2 tables. The query I have created seems to only be finding one record, but what I would really like is display all the records from the project table between project_status 1 - 4, regardless whether the project has many related invoice/purchase records. The related invoice/purchase data should be summed
Thanks
SELECT project.projectID, project.project_title,
sum(invoice.invoice_net), sum(purchase.total_order)
FROM project
left JOIN invoice
ON project.projectID=invoice.projectID
left JOIN purchase
ON project.projectID=purchase.projectID
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
WHERE project.project_statusID BETWEEN 1 AND 5
I'm porting my app from Django to ASP.NET Webforms (against my will, but what can we do with the corporate world..), and I'm used to Django generating all my SQL queries so now I need help.
I have 3 tables: proceso,marcador,marcador_progreso
Every proceso has many marcador_progreso, which in turn is the foreign key table to marcador.
So basically the tables look like:
proceso
id
marcador
id
text
marcador_progreso
id
marcador_id
proceso_id
state
For all the marcador_progreso where its proceso_id is the current proceso (from a QueryField in the URL), I need to list its state and it's respective marcador.text.
I've been working with EntityFramework but this is like a double query so I'm not sure how to do it.
I guess it is something that combines the following two statements, but I'm not sure how to do it.
SELECT [state] FROM [marcador_progreso]
SELECT [text] FROM [marcador] WHERE ([id] = marcador_id)
You want to do a JOIN:
SELECT mp.state, m.text
FROM marcador_progreso as mp
INNER JOIN marcador as m
ON mp.marcador_id = m.id
This is an excellent post that goes over the various join types.
You'll want to know about JOINs to call more than one table in your FROM clause. JOIN combines records from two or more tables in a database by using values common to each. There are different types - the SQL example below is an INNER join, which gets only records where both of the tables have a match on the common value. You may want to consider a LEFT join which would get any records that exist for the LEFT table (in this case marcador), even if there are not any matching record in the RIGHT(marcador_progreso ) table.
Pop the below in Management Studio, Play with different joins. Replace the INNER with LEFT, run it without the WHERE.
Read about `JOIN's.
In general, for your new venture of writing your own queries, they all start with the same basic structure:
SELECT (UPDATE,WHATEVER DML statement, etc) (COLUMNS) what you want to display (update,etc)
FROM (TABLE) where those records live
WHERE (FILTER/LIMIT) conditions that must be met by the data
Happy fetching!
SQL:
DECLARE #ProcessoId int
SET #ProcessoId = --1
SELECT m.[STATE],mp.[TEXT]
FROM marcador M
INNER JOIN marcador_progreso MP ON MP.marcador_id = m.id
WHERE proceso_id = #ProcessoId
EF INNER example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id
where proceso_id == processoIdvariable
EF LEFT example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id into details
from d in details.DefaultIfEmpty()
where proceso_id == processoIdvariable
I have two identical queries save for the position of the left join in the from clause. One runs very slow, the other runs very fast (abbreviated for clarity):
--SLOW
SELECT DISTINCT b.id
FROM a LEFT OUTER JOIN b ON a.id = b.id
JOIN c ON a.id = c.a_id
JOIN d ON c.id = d.c_id
WHERE d.value = 9;
--FAST
SELECT DISTINCT b.id
FROM a JOIN c ON a.id = c.a_id
JOIN d ON c.id = d.c_id
LEFT OUTER JOIN b ON a.id = b.id
WHERE d.value = 9;
My problem is that, using SQLAlchemy, I seem to only be able to create the slow version of the query. Specifically, I am using table inheritance and am trying to run the following code:
return session.query(A).\
with_polymorphic(B).\
join(C).\
join(D).\
filter(D.value_id == 9).\
distinct()
In other words, I don't have control over where the LEFT JOIN is being created.
How do I make SQLite and/or SQLAlchemy smarter about this?
I am not aware of the ways to force SA into changing the query. But I would to identify why there is a difference in execution plan for the query. I would assume that if you have indices on the join columns, the order of the JOINs in the query should not matter.
You can use the EXPLAIN statement to check the execution plan. Although you need to get familiar with The Virtual Database Engine of SQLite. Still it might be possible you spot the difference between two execution plans immediatelly and will be able to improve the database performance instead of tricking the SA.
Try to also remove C or D from the query for even easier comparison of execution plans.