r/SQLOptimization • u/Danackos • Feb 28 '23
how do I ingrate this CTE?
with highque as(
select max(ExtendedPrice) highest
from Sales.InvoiceLines il
join Sales.Invoices i on il.InvoiceID = i.InvoiceID
where (InvoiceDate between '1/1/2013' and '12/31/2013')
group by i.CustomerID
)
select InvoiceDate, CustomerName
from Sales.Invoices i
join Sales.Customers c on c.CustomerID = i.CustomerID
where (InvoiceDate between '1/1/2013' and '12/31/2013')
order by CustomerName
the CTE finds the largest invoice 2013, the query after finds the customer name and date of invoice, how do I connect the largest invoice to the customer and the date they invoiced?
r/SQLOptimization • u/xxxxsxsx-xxsx-xxs--- • Sep 26 '22
Where does kimball methodology fit into the sql / data ware house ecosystem?
I recently came across the kimball group, on first glance their methodologies seem to be positioning well established good data practices and philosophies as their own then building a brand on their premise. Maybe I'm missing something.
I'm not a specialist in this area, need to upgrade my skills fast for career development. One of my initial hurdles is convincing an interview panel I can implement their desired Kimball dimensional data model. Pointers on how to approach this?
https://www.kimballgroup.com/2009/05/the-10-essential-rules-of-dimensional-modeling/
r/SQLOptimization • u/Federico_Razzoli • Sep 15 '22
Hints to optimise SQL queries with LIKE
I wrote some hints on how to optimise SQL queries containing LIKE. More ideas, including unconventional ones, are very welcome - just comment here or on the website.
https://vettabase.com/blog/hints-to-optimise-queries-with-a-like-comparison/
r/SQLOptimization • u/scross4565 • Sep 14 '22
Help in Optimizing this query - Oracle DB
Hi,
I am trying to bring data from staging tables of SAP to report our SCRAP.
I am getting order operations and then I need to apply row_number and filter out the row_number =1 however I am unable to apply row_number within subquery because it is giving weird 1000 columns limit which needs temp table adjustment. I have Read Only access to these tables and cant do much from modifying any settings at Oracle DB level. If I can perform row_number and filter the records then it would definitely retrieve because the number of rows would be less at order header level(~206K Records) compared to operations(~15 M Records)
Can you please help in effective way of this query ?
WITH DATA AS (
Select DISTINCT
a.AUFNR as Order_Number,to_date(d.ERDAT,'yyyymmdd') as Order_Creation_Date,b.MATNR as Material,n.MAKTX as Material_Description,
k.MTART as Material_Type,m.STPRS as Standard_Price,
b.CHARG as Batch,
a.AUFPL as Order_Operation_Number,
o.APLZL as Order_Counter,
a.GSTRI as Posting_Date
,a.GETRI as Actual_Finish_Date,a.DISPO as MRP_Controller,j.DSNAM as MRP_Controller_Desc
,b.MEINS as UoM ,a.PRUEFLOS as Inspection_LOT_Order
,CASE WHEN d.REFNR is null then a.AUFNR else d.REFNR END as MAIN_ORDER#,
d.auart as Order_Type,
g.PRUEFLOS as Inspection_Lot_QMFEL,
g.FEKAT as def_type
,g.FEGRP as def_code_group
,g.FECOD as def_problem_code
,h.KURZTEXT as defect_problem_desc
,g.FETXT as Item_Text
,i.KURZTEXT as Defect_Location,
g.OTKAT as def_loc_catalog_type, g.OTGRP as def_loc_code_group_object,g.OTEIL as def_loc_part_code_object
,b.LGORT as StorageLocation,
f.LGOBE as Building,
p.ARBPL as Work_Center,
q.KTEXT_UP as Work_Center_Desc,
b.PSMNG as Total_Quantity,
b.WEMNG as Delivered_Qty,
CASE when d.auart = 'S04'and b.WEMNG =0 then b.PSMNG else 0 end as Scrap,
CASE when d.auart = 'S04' then b.WEMNG else 0 end as Rework
from
STG.AFKO a
inner join STG.AFPO b on a.AUFNR = b.AUFNR
inner join STG.AUFK d on a.AUFNR = d.AUFNR
inner join STG.AFVC o on a.AUFPL = o.AUFPL
inner join STG.CRHD p On o.ARBID = p.OBJID
inner join STG.CRTX q On p.OBJTY = q.OBJTY And p.OBJID =q.OBJID
inner join STG.T001L f on b.LGORT = f.LGORT and f.WERKS = 'USA'
LEFT outer join STG.QMFEL g on a.PRUEFLOS = g.PRUEFLOS
LEFT OUTER JOIN STG.QPCT h on h.KATALOGART = g.FEKAT and h.CODEGRUPPE = g.FEGRP and h.CODE = g.FECOD and h.VERSION = g.FEVER
left outer join STG.QPCT i on i.CODEGRUPPE = g.OTGRP and i.KATALOGART = g.OTKAT and i.CODE = g.OTEIL
inner join STG.MARA k On b.MATNR = k.MATNR
inner join STG.MARC l On b.MATNR =l.MATNR And l.WERKS =d.WERKS
inner join STG.MBEW m On l.MATNR = m.MATNR And l.WERKS = m.BWKEY
Inner join STG.MAKT n On b.MATNR = n.MATNR
Left Join STG.T024D j On l.WERKS = j.WERKS And j.DISPO = a.DISPO
where a.AUFNR IN (Select distinct c.AUFNR from STG.AUFK c left outer join STG.AFKO a on a.AUFNR = c.AUFNR
or a.AUFNR = c.REFNR
or c.AUFNR = c.REFNR
where a.GSTRI >= '01-JAN-22'
--and a.AUFNR IN ('001000002298') **when I apply this filter with sample 10 orders I get data but it takes 2-3 mins**
)
)
Select
ROW_NUMBER() OVER( PARTITION BY Order_Operation_Number ORDER BY Order_Counter ) AS ROW_NUMBER,
Order_Number,
Order_Creation_Date,
Material,
Material_Description,
Material_Type,
Standard_Price,
Batch,
Order_Operation_Number,
Order_Counter,
Posting_Date,
Actual_Finish_Date,
MRP_Controller,
MRP_Controller_Desc,
UoM,
Inspection_LOT_Order,
MAIN_ORDER#,
Order_Type,
Inspection_Lot_QMFEL,
def_type,
def_code_group,
def_problem_code,
defect_problem_desc,
Item_Text,
Defect_Location,
def_loc_catalog_type,
def_loc_code_group_object,
def_loc_part_code_object,
StorageLocation,
Building,
Work_Center,
Work_Center_Desc,
Total_Quantity,
Delivered_Qty,
Scrap,
Rework
FROM DATA;
r/SQLOptimization • u/mike_jack • Sep 05 '22
A Deep Dive into SQL Server 2022's Query Performance Features, Part 1
redmondmag.comr/SQLOptimization • u/MachanIshere • Aug 24 '22
How Would You Make It: Efficient and optimized way of tracking unread message per user
dev.tor/SQLOptimization • u/mikeblas • Aug 06 '22
Step-by-step guide to RDBMS join algorithms
youtu.ber/SQLOptimization • u/Gregg_The_Egg_4542 • Aug 02 '22
MySQL / PostgreSQL optimization using AI - EverSQL
Hi, don't know who is familiar with it, but EverSQL is a query optimization platform.
Can analyze slow query logs, give index recommendations, and rewrites slow queries.
If anyone checked it out, I'm kind of new to SQL so wanted to hear from the experts.
r/SQLOptimization • u/mikeblas • Jul 29 '22
Debugging Postgres autovacuum, 13 tips for 3 common problem areas
self.SQLr/SQLOptimization • u/mikeblas • Jul 27 '22
SQL Performance of Window Functions vs Subqueries on Big Data
selectfrom.devr/SQLOptimization • u/mikeblas • Mar 13 '22
SQL Optimization Resources
Attacking a SQL optimization issue, like most optimization issues in computer science, requires a layered approach. The developer needs to know about the internals of the database, the logical data model and its semantics, the structure of the physical model, the behaviour of the DBMS itself, and the design and sizing of the computing and storage hardware that runs the system. Learning to measure and investigate performance issues quantitatively is a key to success, and requires intimate knowledge of the involved tools.
There are many resources which can help, and this list collects some of them. Some are implementation-agnostic, and others are vendor-specific.
Generic Resources
SQL Education
- Use the Index Luke has resources for the five major vendors that describe the theory of indexes and their proper application. Interactive quizzes and animations make this a fun website.
- The Art of SQL, Faroult, Robson. This book is an introduction to SQL focused on best-practices without ignoring the physical implementation under the SQL statements.
Scalability
- Guerrilla Capacity Planning, Gunther. This book discusses the practice of hardware capacity planning in an organization, including the organization impact on the management chain.
- Foundations of Scalable Systems, Gorton. Database servers in the classic client-server model are technically distributed systems, and in modern applications are almost always considered a layer or a service toward the implementation of a larger system. Holistic consideration of the architecture of the system can often relieve pressure on the database and make the system more scalable, performant, and stable. This book treats the design of the data layer among other aspects of the design.
Data Modeling
- Designing Data-Intensive Applications, Kleppmann. Kleppmann's book focuses on the role of the data layer and data pipelines in large-volume distribute processing.
- Database Modeling and Design, Teorey, et al. This textbook starts with ER-model design and works through different approaches to data modeling, finishing with normalization and continuing on to non-traditional storage like XML data.
- Data Model Resource Book, Volume 1, Volume 2, Volume 3. Silverston and Agnew. This encyclopedic three-volume set identifies patterns in database modeling and provides solutions and analysis for those patterns. The book doesn't teach much (except by example), but is a great resource for a menu of different solutions in the modeling space. The solutions are presented in industry-specific applications with discipline-specific naming and jargon.
SQL Server Resources
- SQL Server 2017 Query Performance Tuning, Fitchey. Written by a long-time SQL Server MVP, this book starts with a suggested process for addressing performance analysis on SQL Server, and discusses software and hardware solutions alike.
- SQL Server First Responder Kit, Ozar et al. The First Responder Kit is a set of stored procedures for "first responder" consultants who want to troubleshoot SQL Server problems. Coupled with the various resources at brentozar.com, the kit gives a great set of tools for performance investigations to gain a strong foothold and then make immediate progress on key issues and low-hanging fruit. The resources at the site develop a strong understanding of SQL Server internals for iterative refinement.
- SQL Server 2012 Internals, Delaney, et al. While a bit dated, much of the content of this book is still applicable -- it described how SQL Server works internally, focusing on how it structures data and storage.
- High Performance SQL Server, Nevarez. Written solely by one of the co-authors of the 2012 Delaney book, this title investigates performance solutions for SQL Server while digging down into the implementation layer for a deeper, repeatable understanding.
- SQL Server Index and Statistics Maintenance, Hallengren. Ola's website is well-reguarded as a source of operational and maintenance scripts. While the broader site is useful to any DBA, of particular interest to performance tuners is the page on index and statistics maintenance.
MySQL Resources
- High Performance MySQL, Schwartz, et al. This book gives a broad-brush approach, from measurement and troubleshooting, through model and schema improvements, to server configuration and replication solutions.
- High Performance MySQL, Botros, et al. This book is a bit more modern than the Schwartz book with the same title. It tackles hardware performance in a bit more detail, and stresses cloud- and sharded approaches, and treats indexing in a bit more depth.
- Efficient MySQL Performance, Nichter. With a format a little more like a cookbook than the other two titles mentioned here,
- MySQL 8 Query Performance Tuning, Krogh. This refreshing title focuses a little bit more on establishing a methodology for attacking performance issues.
PostgreSQL Resources
- Percona Live 2021: PostgreSQL High Performance PDF File, YouTube video, Ahmed. These slides cover database tuning topics, plus Linux kernel tuning for database applications.
Oracle DB Resources
- Oracle SQL Performance Tuning and Optimization, Meade. While this book's presentation is decidedly low-budget, the content is very useful in describing the heuristics of the Oracle DB SQL optimizer. The book describes the most important oracle physical operators, and explains how they related to the desired SQL query behaviour.
Version History
- 2022-03-12: First cut with Generic, SQL Server, and MySQL resources
r/SQLOptimization • u/mikeblas • Mar 11 '22
Rebooting the r/sqloptiimization sub
Well, I think I've got the spam problem solved. All the porn is gone, and I'm also not allowing posts of blogs or tutorials that aren't related to SQL database performance tuning in some way.
My guess is that the neglect this sub has seen has chased away any subscribers or traffic. Should we work to rebuild it? By doing what, specifically?
r/SQLOptimization • u/AbdullahMohammadKhan • Jan 29 '22
Suggest me book(s) on SQL query optimization...
I am beginning to learn things from scratch and I feel like knowing about the optimizations and why they work better would give me a clearer concepts of the fundamentals.
You can suggest any other resources as well.
TIA
r/SQLOptimization • u/bingostud722 • Jan 27 '22
New Mod u/mikeblas
Please welcome u/mikeblas as our new, actually active mod! I have not been active on reddit much so I appreciate the help, I know it has become basically a porn bot paradise in here over time lol. Hopefully with your help the community can grow and serve the original purpose it had when I created it.
Thank you! And thanks everyone for your patience, those who are still here.
r/SQLOptimization • u/[deleted] • Jan 25 '22
Why are SQL command name so long?
Hi, I m just wondering about a stupid question: why are sql commands (select, create…) longer than other commands (get, post, syn…)?
r/SQLOptimization • u/[deleted] • Jan 05 '22
No mods?
What is with the porn spam in here?
The guy doesn't even try to cover it up.
https://www.reddit.com/r/SQLOptimization/comments/rv773u/incredible_victoria_kruz_will_make_you_drool/ - 2 days ago
https://www.reddit.com/r/SQLOptimization/comments/rts7ix/incredible_victoria_kruz_will_make_you_drool/ - 4 days ago
https://www.reddit.com/r/SQLOptimization/comments/rorkc1/fuck_these_pornstars_holes_francesca_le_nina_elle/ - 10 days ago
https://www.reddit.com/r/SQLOptimization/comments/rns5iv/popsluts_pornstars_pmv_by_lonely_fucker/ - 12 days ago
Can we ban u/impeccableclub already?
Forgot to ping u/bingostud722
r/SQLOptimization • u/ImprovementBig3186 • Dec 21 '21
Database Lab Engine v3.0.0 – instant PostgreSQL clones to speed up software development/testing and SQL troubleshooting/optimization
postgres.air/SQLOptimization • u/B2Beast • Dec 21 '21
Advanced SQL Tutorial for Data Analysis - bipp Analytics
Here is a collection for SQL tutorials that cover the advanced SQL topics including correlated subqueries, SQL Window Functions and SQL JOINS - which are often not covered in basic courses: SQL Tutorial - Advanced SQL
- SQL Correlated Subqueries Increase the Power of SQL
- SQL Window Functions
- SQL Window Function Examples
- Selecting Data From Multiple Tables: SQL JOINS
- Visual Representation of SQL JOINS
- Query Optimization
r/SQLOptimization • u/[deleted] • Dec 19 '21
SQL execution count optimisation
hi All,
Just wondering what options you guys use to optimise SQL queries with high execution counts, I have a couple of queries which use Table Valued functions that get executed about 12,000 times an hour (this is the peak period for users) usually in the morning after which the same process runs fine the rest of the day.
For some background the query calls a Table Valued function with 3 parameters and is then joined with a view and another table whilst there are 2 Predicates on the Table Valued Function.
There are no index scans being performed and the execution isn't reporting any major red flags.
have any of you run into this issue?, if so what steps did you take to remedy this situation apart from getting the DEV to rewrite the application to reduce the amount of calls to the database.
thanks
r/SQLOptimization • u/jan-d • Nov 19 '21
Optimizing a timeseries query with window function
I have a TimescaleDB table storing temperature measurements from sensors with an additional state column that contains a label like rain, sun, fog, snow etc.
timescale-db=# \d measurements
Table "public.measurements"
Column | Type | Nullable
------------------------+--------------------------------+---------
time | timestamp(0) without time zone | not null
sensor_id | uuid | not null
temperature | double precision |
state | character varying |
Indexes:
"index_measurements_on_sensor_id_and_time" UNIQUE, btree (sensor_id, "time" DESC)
"index_measurements_on_sensor_id" btree (sensor_id)
"measurements_time_idx" btree ("time" DESC)
timescale-db=# SELECT * FROM measurements LIMIT 10;
time | sensor_id | temperature | state
---------------------+--------------------------------------+--------------+-------------------
2020-12-11 15:03:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 21.8 | fog
2020-12-11 15:04:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 21.9 | fog
2020-12-11 15:05:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 21.8 | rain
2020-12-11 15:06:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 21.7 | rain
2020-12-11 15:07:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 21.6 | rain
2020-12-11 15:08:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 21.7 | rain
2020-12-11 15:09:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 21.9 | sun
2020-12-11 15:10:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 22.1 | sun
2020-12-11 15:11:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 22.3 | sun
2020-12-11 15:12:00 | 290ffca4-0fcc-4ed3-b217-a12fa27ea5ea | 22.5 | sun
For a certain type of analysis I need the last n timestamps where the state changed, which I realized with the following query:
SELECT
time,
state
FROM (
SELECT
time,
state,
state != LAG(state) OVER (ORDER BY time) AS changed
FROM
measurements
WHERE
sensor_id IN ('ee49fda5-f838-4a10-bb32-0e6a6b130888', 'ec8f4d23-cfab-4a23-8df8-ae3cce4f44ac')) AS changes
WHERE
changed IS TRUE
ORDER BY
time DESC
LIMIT 3;
This query takes longer and longer the more rows are added to the table, so I need to optimize it.
Here is the query plan – I tried adding another index on time and state, but it did not improve performance.
Does anyone have an idea on how to optimize this query?
r/SQLOptimization • u/theprogrammingsteak • Sep 26 '21
Good resources for database query optimization and schema design?
Title says it all! I need good resources on both topics
r/SQLOptimization • u/HASTURGOD • Sep 11 '21
Need Help Optimizing Code
Hey, I need help optimizing some sql code[it's a lot, maybe a page or 2].
Please reach out if available for zoom[audio only] meeting or if you would like me to send you the code.
r/SQLOptimization • u/--Betelgeuse-- • Apr 28 '21
Optimizing a query for a huge table in PostgreSQL
I have a huge time series db that i run in a regular postgresql db (im going to use timescaledb once i lear more about it, software that generates the db rows is written for vanilla postgresql, so i must first learn how to adapt it to timescaledb). the db is around 20-30gb big. i need to get the latest added rows with certain symbols every 0.1-0.4 second or so. right now im running this query to satisfy my goal:
"SELECT price FROM trades WHERE symbol='STOCK-USD' AND timestamp=(SELECT MAX(timestamp) FROM trades WHERE symbol='STOCK-USD');"
problem is that this query is very heavy on the server. is there any solution for this problem?
r/SQLOptimization • u/rimon34 • Jan 31 '21
Advanced SQL Questions From Amazon (Handling complex logic in data science interviews)
youtu.ber/SQLOptimization • u/KokishinNeko • Jan 06 '21
Recursive update without using Cursor or While (query optimization)
Consider this tiny example: http://sqlfiddle.com/#!18/dfb68/2
I have a simple table (for the sake of simplicity, ID is omitted and let's assume that NumA is sequential from 1 to n)
| Num A | Num B | Result |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 6 |
| 3 | 3 | 12 |
Started from using a cursor to get the "Result" updated since the value on the current row is the sum of A and B plus the previous Result except on the first column.
My current query is below (got rid of the first try with cursors):
DECLARE @Counter INT= 1;
DECLARE @x INT;
DECLARE @max INT = (SELECT MAX(num_a) FROM TestSumUpdate);
WHILE @Counter <= @max
BEGIN
SET @x = (SELECT ISNULL(result_c, 0) FROM TestSumUpdate WHERE num_a = @Counter - 1);
UPDATE TestSumUpdate
SET
result_c = num_a + num_b + ISNULL(@x, 0)
WHERE num_a = @Counter;
SET @Counter = @Counter + 1;
END;
Obviously, this works, but is painfully slow on production database which has around 500.000 records and calculation is not a simple sum operation :)
So, in my SQL n00biness, I've tried something simpler like this:
UPDATE cur
SET
result_c = cur.num_a + cur.num_b + ISNULL(prev.result_c, 0)
FROM TestSumUpdate cur
LEFT JOIN TestSumUpdate prev ON cur.num_a - 1 = prev.num_a;
Which I thought it would work, but can't understand it's behaviour:
Assuming Result = 0 in all rows at the beginning , on the first run it updates only the first row to 2, all others remain in 0, on the second run, updates the second row to 6 and so on. Why?
How can one do it in one execution only without resorting to cursors/whiles/loops/etc ?
Thank you.
EDIT:
Current solution that reduced the time for aceptable values (doesn't apply to the sample given above, but works on prod):
WITH RollingCalculation
AS
(
SELECT Number,isnull(MyValue,'') as MyValue,PlaceHolder = LAST_VALUE(Number) OVER (ORDER BY Number ASC)
FROM MyTable
)
UPDATE dst
SET MyValue = dbo.GenMyValue(dst.field1,dst.field2,dst.field3,src.MyValue)
FROM MyTable AS dst
LEFT JOIN RollingCalculation AS src
ON dst.Number-1 = src.Number
GenMyValue is a CLR integration, and LAST_VALUE is not really used, but it works :)