Ms access hangs up after left join - ms-access-2010

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.

Related

Using querys to get information from different tables

I am learning SQLite and I'm using this database to learn how to correctly use querys but I'm struggling specially when I have to use data from multiple tables to get some information.
For example, with the given database, is there a way to get the first name, last name and the name of songs that every customer has bought?
All you have to do is a simple SQL SELECT query. When you say you're having trouble getting it from multiple tables, I'm not sure if you're trying to get the data from all of the tables in one single query, as that is not necessary. You just need to have multiple instances of the SELECT query, just for different tables (and different column names).
SELECT firstName, lastName, songName FROM table_name
You have to study about JOINS:
select
c.FirstName,
c.LastName,
t.Name
from invoice_items ii
inner join tracks t on t.trackid = ii.trackid
inner join invoices i on i.invoiceid = ii.invoiceid
inner join customers c on c.customerid = i.customerid
In this query there are 4 tables involved and the diagram in the link you posted, shows exactly their relationships.
So you start from the table invoice_items where you find the bought songs and join the other 3 tables by providing the columns on which the join will be set.
One more useful thing to remember: aliases for tables (like c for customers) and if needed for columns also.
You need to use joins to get data from multiple tables. In this case I'd recommend you using inner joins.
In case your are not familiar with joins, this is a very good article that explains the different types of joins supported in SQLite.
SQLite INNER JOINS return all rows from multiple tables where the join
condition is met.
This query will return the first and last name of customers, and the tracks they purchased.
select customers.FirstName,
customers.LastName,
tracks.name as PurchasedTracks from invoice_items
inner join invoices on invoices.InvoiceId = invoice_items.InvoiceId
inner join customers on invoices.CustomerId = customers.CustomerId
inner join tracks on invoice_items.TrackId = tracks.TrackId
order by customers.LastName

Join operation between Table and View

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.

Indexing columns crossing multiple tables

I have 2 tables, headers with 2 millions rows and files with 30 rows.
I have a query that supposes to get the total number of headers for each directory.
The SQL looks like below:
SELECT files.dir_id, COUNT(*) AS "TOTAL"
FROM headers
LEFT JOIN files ON headers.file_id = files.file_id
GROUP BY files.dir_id
Currently, executing the SQL above is taking 20sec. How can I index it to make it faster?
I have tried CREATE INDEX IF NOT EXISTS HEADERS_FILE ON HEADERS(FILE_ID). This made GROUP BY file_id gets instant response (without left joining files table). However, it doesn't improve the performance for the original query above.
I'm thinking of something like CREATE INDEX INDEX_NAME ON HEADERS, FILES(FILE_ID, DIR_ID) should work. but I find no way in creating such index.
Appreciate for any help. Thanks!
The LEFT join prevents the database from using files as the outer table in the nested loop join.
Try using an inner join, and then adding the missing rows by hand; this might allow better optimization for the two subqueries:
SELECT files.dir_id, COUNT(*) AS "TOTAL"
FROM headers
-- LEFT JOIN expanded by hand for better optimization
INNER JOIN files ON headers.file_id = files.file_id
GROUP BY files.dir_id
UNION ALL
SELECT dir_id, 0
FROM files
WHERE file_id NOT IN (SELECT file_id
FROM headers)

Combining data from two SQL queries

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

SQL Server Creating Indexes Catalogs with joined tables

So at the moment I am building a Advanced Search in .NET, and getting the results is just proving a bit slow so was looking at creating indexes on the tables.
I.e went to tables and define full text index.
So now I have my catalog with the 5 tables and selected columns.
But I cant see how this catalog actually joins these tables ?
I.e. in my "slow" stored procedure I could have
select *
from table1
inner join table2 ON table1.id = table2.linkedID
etc for other tables ?
and now I guess I can go
select * from catalogName
but how does catalogName know what columns to join for the inner join etc
You don't query the fultext catalog directly, you use the fulltext functions in your query, like CONTAINS, CONTAINSTABLE, FREETEXT and FREETEXTTABLE:
SELECT field, field, field
FROM table
WHERE CONTAINS(field, 'some text');
Full text has nothing to do with joining tables and if your query is slow because you join 5 tables then FT is unlikely to help at all.

Resources