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)
Related
I'm coming from SQL Server enviroment where you can declare a temp table with #table, but as I've read you can't do this in oracle.
I want get a value for 500.000 hardcoded id's from a table, but as the IN clause has a limit of 1000 I need to find another way. Is the best way to create a temporary table and insert the hardcoded values and then join the other table which contains the values I need ?
My client (toad) has autocommit set to off and I dont want to commit anything, I want it to be session-based so when I close the database client I want the temporary table do disappear. Is the code below the right way to do in oracle?
CREATE GLOBAL TEMPORARY TABLE Test(HardcodedId number(10))
ON COMMIT DELETE ROWS;
I've also tried to use inner join and in the join select the hardcoded values from dual, but this creates a column for each value and i'm not able to use a reference to join with. Is it possible to insert all values into a single column in dual?
You can use some thing like this (500 union all)
select * from (
select '1' from dual
union all
select '2' from dual
...) q
Then you can join this with other tables.
For your situation, I would use a GTT (global temporary table) - which you have already researched by the looks.
The advantage of a GTT is that it's a permanent object (so no need to constantly create and drop it) and the data "stored" in it is on a session basis.
I execute a query in my DB:
SELECT table1.*, tabl2.* FROM table1 JOIN table2 USING(id);
In these 2 tables i have a common column "id". What I have to ask, in order to get the column 'id' once time in the results and not twice?
I thought one solution is to write down in the query which columns I want. But If I want to avoid this (as there are many) ?
Will it work for you to name specific columns you need from both tables? something like:
SELECT table1.id, table2.other_column1, table2.other_column2 FROM table1 JOIN table2 USING(id);
You are selecting all fields from both tables by using (*)
I'm working on a sqlite database and try to make a special request between two tables.
In the first table (table1 for example), i have two columns named "reference" and "ID". I want to search an ID in it, get it value in "reference" and display all informations from the table which have this value as name.
I try to find something on the internet but I didn't find an answer.
This is the request I made:
select * from (select Reference from table1 where Name='Value1')
It only give me the result of
select Reference from table1 where Name='Value1'
EDIT:
I want
select Reference from table1 where Name='Value1' => name of table
select * from name of table => show all elements
I'm new in sqlite but I hope you can help me.
Thank you by advance
Matt
If I understand your question correctly, I don't think there's a way to do it in sql completely (or at least not in a portable way). I'd recommend one of 3 solutions:
Do exactly what you want, but do some processing in Python. That means query your master table, then construct new query based on each of the rows returned.
If you have many tables, possibly changing dynamically - it may be a good idea to rethink your database design. Maybe you can move some of the changing table names into a new column and put your data in one table?
If you have only a few tables available as the Reference and they never change, you could join all the possible tables, like:
SELECT ... FROM table1
LEFT JOIN table2
ON table1.id = table2.id AND table1.Reference = "table2"
LEFT JOIN table3 ...
But you may need to explain it all a bit better...
I have database with with roughly following structure:
table1 (name) -< table2 -< table3 (score)
where -< means 1 to many relationship. What i need to do is for every string in a given list find the linked entry from table3 with a maximum score value. The way i do it now is quite slow, and i wonder of it could be sped up.
How i am doing this:
SELECT k.score,k.yaw,k.pitch,k.roll,k.kp_number,k.ke_number,k.points,k.elems --various fields of third table
FROM File
JOIN FaceDetection AS d ON d.f_id=File.file_id --joining second table
JOIN FaceKey AS k ON k.face_det=d.fd_id --joining third table
WHERE name=:fld
ORDER BY k.score DESC
I open transaction, prepare query with the above text, and in cycle retrieve the entries i am interested in from the database, then commit transaction. What are better, faster ways?
Indexes can be used for all the columns that are used for lookups or sorting, but a query cannot use more than one index per table.
Check the EXPLAIN QUERY PLAN output to see whether this query does table scans or uses indexes.
You are not returning values from any table but FaceKey, so you do not actually need to do a join.
However, rewriting the query as below might or might not help:
SELECT score,
yaw,
pitch,
roll,
kp_number,
ke_number,
points,
elems
FROM FaceKey
WHERE face_det IN (SELECT fd_id
FROM FaceDetection
WHERE f_id IN (SELECT file_id
FROM File
WHERE name = :fld))
ORDER BY score DESC
If I have this statement,
SELECT table1.*, table2_1.`values` AS val_1
FROM table1 JOIN table2_1
ON table1.row_names=table2_1.row_names
I would actually like this the result joined back into table1. Any inclusion of a join statement after SELECT and before FROM gives me an error.
Why can I not save the results to a table, and is it possible to save it back to one of the original tables?
Because you do it the wrong way. SELECT statement will not modify you data no matter how much you want it. If you want to modify data, you need to use UPDATE statement.