Questions tagged [query-optimization]

For questions about making database queries run faster. PLEASE INCLUDE QUERY PLANS, AND TABLE AND INDEX DEFINITIONS in your question. Please use additional tags to specify your query language and server maker. Some of these questions may belong on https://DBA.stackexchange.com, especially if they involve reindexing, query plans, etc.

Filter by
Sorted by
Tagged with
1 vote
1 answer
21 views

Speeding up a mySQL JOIN query

I'm trying to see up a query in mySQL. The table structure looks like: CREATE TABLE `glinks_BuildRelations` ( `relation_id` int(11) NOT NULL, `cat_id` int(11) NOT NULL, `link_id` int(11) NOT ...
  • 4,851
0 votes
0 answers
10 views

Performance Issue with C# MongoDB Driver Full Text Search

I'm facing performance issue with MongoDB C# Driver while fetching documents from the Database. The database size is around 212 GB with 228K records . There are 2 fields (HistoryXML, HistoryPassage) ...
0 votes
0 answers
21 views

How do I optimize this stored procedure to improve speed of fetching data?

Below is the stored procedure I am referring to. /****** Object: StoredProcedure [dbo].[usp_GetReportDetails] ******/ IF OBJECT_ID(N'[usp_GetReportDetails]', N'P') IS NOT NULL DROP PROCEDURE [dbo]...
0 votes
1 answer
41 views

Optimize Oracle query to include only one statement and make the script run faster

I have this sequel where I'm listing all my deals, where I'm only interested in those deals that are not booked by Interface and where the Source Interface = TBG. I want to add another statement where ...
1 vote
1 answer
27 views

MySQL 8: Query Performance between subquery with GROUP BY and INNER JOIN with GROUP BY

I have to retrieve a list of postings, by one or more category IDs. I do not want to have duplicate postings in my results. I'm only interested in responses that directly relate, or can draw relations ...
1 vote
1 answer
44 views

How is Sql Server able to use Clustered Index Columns as Seek Predicate when Seeking on Nonclustered Index

Noticed a strange behavior and was hoping someone could point me to documentation or describe how this is even possible. I have a table (A,B,C) with a clustered index (A,B) and a nonclustered index (A,...
0 votes
0 answers
27 views

Same instance of database with same data is too slow postgresql

I use Postgresql v12.11 for my application Now I have performance issue in a table with 4M rows of data. I export my database and create new postgresql container with same version and same memory and ...
0 votes
1 answer
18 views

MySQL Full Text Searching -- is it possible to speed up query with additional where clauses or joins?

I've noticed that full text searches in MySQL do not seem to speed up when reducing the search pool with additional where clauses or joins. For example: SELECT * FROM `blog_posts` WHERE `id` = 1 AND ...
0 votes
1 answer
36 views

Best way to optimise a simple MYSQL COUNT query

I have a table called signups and all I want is the COUNT of all signups where is_email_verified is true. Attempt 1 results in an index scan: SELECT COUNT(*) FROM signups WHERE is_email_verified = ...
-1 votes
0 answers
32 views

Window function with subquery results to unexpectedly slow query plan

This is my victim query SELECT *, lag(quantity) over (partition by product_id, size_id, warehouse_id order by size_id, warehouse_id, created_at ASC) - quantity as orders FROM ...
  • 51
-1 votes
0 answers
33 views

Optimize simple join query

I have two tables t1 and t2. The t1 table contains 20 records. t2 contains 26.000.000 records and have a many fields. t2 table size is 6 Gb. field2 is the primary key. Problem with the simple query: ...
0 votes
0 answers
58 views

MYSQL query taking a huge time compared to other databases for getting most recent in a group query

I am trying to run a query on mysql database which seems to be taking a lot of time, when I run the same query in sqlite the time taken is very less. Environment -> Python36, WSL2 Sqlite Version -&...
0 votes
0 answers
17 views

Does django have lazy queries in foreign reverse lookup? [closed]

I have this code: def get_percentage_completions(task: Task, date_start: date, date_end: date): result = {} execution_percents = task.execution_percents.all() if execution_percents.exists(...
-1 votes
1 answer
37 views

MS Access: Can I speed up this SQL Query?

Tables: Sales Quantity Daily Data Stock Quantity Weekly Data I want both tables to be displayed (even when stock is empty) as long as there's daily data, so I used a left join. SELECT ...
-1 votes
0 answers
21 views

Seqeulize - Return rows that matches as at least one record in the included models

Assuming we have 3 tables A, B, and C where A has many relationships with B and C. I want to get all rows from A table that matches at least one record in Either B or C. If it has to match at least ...
-1 votes
0 answers
38 views

Retrieval is slow from Postgres

I am trying to fetch a text column which is holding json object as value, and as the number of rows increases the retrieval is very slow. I am using postgres v12 as database. A simple query select ...
-1 votes
2 answers
81 views

Optimize subquery (without using Select inside Where clause)?

I am trying to see if there is a way to optimize bottom T-SQL code. This is how previous co-worker wrote, and I am not sure if there is a better way to write because it has Select within the Where ...
  • 1,176
1 vote
1 answer
44 views

Optimize/Improve Performance of PSQL Query

Is there any way to optimize the below query further? select *,RANK () OVER (PARTITION BY jsondata->> 'id' ORDER BY pid desc ) as rank_number from xyz where jsondata ->> 'id' in (select ...
  • 11
0 votes
1 answer
24 views

BigQuery comparative performance with naive ordering by table size

The general advice for optimising BigQuery joins from the docs is as follows As a best practice, place the table with the largest number of rows first, followed by the table with the fewest rows, and ...
1 vote
3 answers
53 views

Oracle Join on V$ views becomes extremely slow when a temp table is used

I am trying to get some information from the Oracle V$ views SELECT bpd.handle,bpd.media,bpd.copy#, bpd.bytes, bpd.session_key, bsjd.operation,bsjd.start_time, bsjd.end_time,bpd.completion_time, bsd....
  • 1,574
1 vote
0 answers
40 views

How can I optimize this 2.x style query for SQLAlchemy?

I am having trouble running this query -- I did have success once which took about five minutes, but all other attempts have timed out. That one time did NOT have the first condition listed in the ...
  • 13
0 votes
0 answers
32 views

Optimal query/join for joining scd-type-2 dimension with facts for reporting/aggregation

I have a fact table and an scd-type-2 dimension table. I want to produce sales report by region and year. I have working solution with a query that joins them for reporting purposes. When I run the ...
  • 14.7k
1 vote
1 answer
61 views

Slower Oracle Query when called by JDBC instead SQL Developer

I am using the JDBC driver "oracle.jdbc.driver.OracleDriver" The server URL ist passed as: "jdbc:oracle:thin:@//server:port/VMP" I have a 3 columns in the table: Additionally ...
0 votes
1 answer
40 views

Parallel sequential scan on compicated join

I have following SQL (simplified and anonymized for easier understanding) select max(db.tableA."id") from db.tableA left outer join db.tableB on (db.tableB....
  • 540
0 votes
0 answers
63 views

Postgresql Update Query Takes To Much Time To Update Data

I am using PostgreSQL 13.4 and has intermediate level experience with PostgreSQL. I have a table (usage_data) which stores log details for our users. This table contains 100+ Million records. Table ...
-1 votes
0 answers
43 views

How to improve the performance of pyodbc or sqlalchemy when running sql scripts?

I have a few SQL scripts that I want to run with Python. When I run the script in sql server management studio (SSMS) on my laptop, it takes about 30 seconds. However, the same script will run for ...
  • 1
-1 votes
0 answers
34 views

MongoDB Query for filtering documents with cron string key matching current time

Here is a sample document from a MongoDB collection Tasks : { task_id : Object("345672fd23"), task_type : "cron-type-job", task_cronstr : [ "45 23 * * 6", &...
  • 261
1 vote
1 answer
41 views

MySQL doest not use indexes on subquery (exists)

why does MySQL not use the (expected) indexes when I query with a subquery (exists)? I also tried it with an JOIN but happens the same, that MySQL does not use the indexes. How can I improve this? ...
  • 835
0 votes
0 answers
70 views

Synapse SQL upsert a big table [closed]

I want to optimize the upsert I am doing on my synapse SQL pool table: Source table is a synapse temp table ROUND_ROBIN with CLUSTERED COLUMNSTORE INDEX Target table is a synapse physical table ...
0 votes
0 answers
53 views

Make postgresql query faster

I have a Query that filters Events by different properties. The query was tested with 1 million events and it took 13 seconds to complete the request. There are two Queries. The first Query filters ...
  • 222
0 votes
1 answer
30 views

Comparing Physical Plans with PushedFilters vs. PartitionFilters in a Spark query

I have thought of three slightly different ways to query a Spark table to filter the data for a date range (I use it mostly to query one day's worth of data) that I am interested in. When I review the ...
  • 2,438
0 votes
1 answer
51 views

Joining with a json column makes the result very slow

There are 3 tables and I linked three tables with one query. But the problem is that the result returns in 7-8 seconds. One of the columns I join is JSON type and the data in it is json. For this ...
  • 11
0 votes
1 answer
27 views

Efficient query for intersections of shared relationships in Neo4J

We have a Neo4J (5.2.0) database containing two types of nodes Topic and Document. Topics and documents are linked by OCCURS_IN relationships (:Topic)-[:OCCURS_IN]->(:Document) and ...
  • 25
0 votes
1 answer
77 views

MySQL query with self-join scans millions of rows

I have an application to work on employee shifts for factories. I have a SQL query to find out if shifts have conflicts: if you have a shift 08:00-16:00 and I assign another shift 09:00-17:00 on the ...
  • 1,141
1 vote
2 answers
62 views

How to make SQL only read CTE referenced in IN clause once?

WITH RootItemIDs AS ( SELECT DISTINCT ParentID FROM C_Item WHERE ParentID NOT IN (SELECT ItemID FROM C_Item) ) SELECT COUNT(*) FROM C_OrderHeader oh JOIN C_OrderDetail od ON oh.OrderID =...
  • 3,165
0 votes
2 answers
51 views

Mysql query not optimized (union all query)

here i need an help like how to optimize this union all query the table has index for where clause column but it's not picking it up and i even tried with force index none of that works any better way ...
  • 91
2 votes
2 answers
61 views

Laravel/MySQL: Very high query runtime

In my laravel project I am currently doing some testing with database seeders to check the performance with many database entries. In my project I have a Model FlightView using a SQL view as database ...
0 votes
2 answers
39 views

Huge performance drop for LIKE statement if column order changes in composite index [closed]

Edit: EXPLAIN result for the first two queries when using src_cus_lang: EXPLAIN result for the last two queries when using cust_lang_src: Original post: I'm using MySQL and have a table as follows: ...
  • 144
0 votes
1 answer
37 views

Linq - improve .Max()-query to execute everything inside the database instead of the C# app

The importent part of our DbVersion model looks like this: public class DatabaseVersion { ..... public int Major { get; set; } public int Minor { get; set; } ......
  • 1,581
0 votes
0 answers
52 views

Optimize SQL query (get rid of rows before sorting in SQL)

I have a PostgreSQL server which is used as a database for a sub-realtime application. Currently a particular table contains 2512742 rows of data, and is updated every minute. This data is frequently ...
0 votes
1 answer
52 views

MySQL left joins query tuning

When I was optimizing MySQL query with inner joins and left joins, I realized that joining tables using id from subqueries is much faster in my case. I use MySQL 5.7. Id columns from all table except ...
  • 31
1 vote
1 answer
30 views

Using IQueryable.Contains inside of the query: would this IQueryable get executed multiple times?

I have a IQuearyable: var ids = entities.Select(s => s.Id).OrderBy(s => s); If it is used to filter down when retrieving other entities: EntityService .For<OtherEntity>() ...
-1 votes
2 answers
54 views

Performance optimization of order by Random in Snowflake

There is a multi CTE query in DBT and one of the CTE contains order by random() in the query. Does it has any impact on the query processing time or can it be removed to optimize the query?
1 vote
1 answer
10 views

Remove Correalted Subquery with Not Equal Join

SELECT (SELECT COUNT(0) FROM TestTable s_t WHERE s_t.Test_type_id <> t.Test_type_id ) AS count FROM TestTable t This is taking time eventhough index exists on table. Help needed in removing ...
  • 11
-2 votes
3 answers
64 views

How to make this query faster: TOP N over TOP 1 in past + all in future

I have a table that contains some data. There are some attributes that are used to 'group' the rows and there is a date column. I'm trying to improve the performance of a query that basically gets TOP ...
  • 1,063
0 votes
2 answers
29 views

why Postgresql Explain Cost is Low in Limit and Result Phase but high in Index Scan phase

In the shared plan index scan cost is very high (cost=0.56..696715.50 But Limit (cost=0.56..224.59 is very low and so on result cost Result (cost=224.59..224.60 rows=1 width=8) why it is so? Right ...
0 votes
1 answer
47 views

MariaDB reviews a lot of rows when using index on uuid with null values

We got a database with a card table CREATE TABLE `card` ( `id` char(36) NOT NULL, `parent_card_id` char(36) DEFAULT NULL, PRIMARY KEY (`id`), KEY `table_parent_card_id_idx` (`parent_card_id`) ...
  • 111
0 votes
0 answers
18 views

Aurora PostgreSQL -Poor Execution Plan

We have a Query which do left join with multiple tables using two columns and Index exist on it rather than doing Index scan, execution plan is choosing Table Scan on the table and to improve query ...
0 votes
1 answer
39 views

pl-sql optimizing to calculate bewteen two transaction time

main question is how could I write optimized query to calculate time stamp between two transaction. Actually there is gigantic number of transactions in one table and I want to calculate time stamp ...
-2 votes
1 answer
40 views

How to optimize a MySQL query to get data from one table which is not present in another table?

I have two MySQL tables named "table1" and "table2", with "table1" containing 1 million rows of data and "table2" containing 0.76 million rows of data. I want ...

1
2 3 4 5
151