query-optimization

TSQL view select optimization when function is present

时光毁灭记忆、已成空白 提交于 2019-12-11 04:19:34
问题 I have this simple SQL as a source in a SSIS task: Select * from budgetview the source is: CREATE VIEW [dbo].[BudgetView] AS SELECT DISTINCT Country, SDCO AS Company, SDAN8 AS Customer, SDLITM AS PrintableItemNumber, dbo.fn_DateFromJulian(SDIVD) AS Date, SDPQOR/100.0 AS Quantity, SDAEXP/100.0 AS Value, SDITWT/10000.0 AS Weight FROM dbo.F553460 There are NO advices for indexes, every thing seems optimized. The function fn_DateFromJulian source is: CREATE FUNCTION [dbo].[fn_DateFromJulian] (

MySQL problem - COMPLETE server overrun! Please advise

本小妞迷上赌 提交于 2019-12-11 04:13:19
问题 This is my first question here so please don't be tough on me... :P I usually tend to google my way to a solution but now I'm in a hurry and have used up all the resources I had... because my website is a commercial one, it would be fair to go ahead and pay someone to help me but... I have some financial problems myself, this was the 3rd month of payment to my dedicated server, still it was only the first month since my site was actually up... I have made it all by myself because I cannot

Optimizing queries with acts_as_taggable_on

微笑、不失礼 提交于 2019-12-11 04:04:27
问题 Using Rails 3.1 and gem 'acts-as-taggable-on' version 2.1.1. I have a class: class Meal < ActiveRecord::Base acts_as_taggable_on :foods ... end I have several different scopes on Meal that I use on a dashboard-type page. In the controller, I call, for example: def index @today = Meal.from_today @yesterday = Meal.from_yesterday end I iterate over @today and @yesterday separately on the dashboard page. I'd like to optimize the database calls. Right now, I call <%= meal.food_list %> in the view

How to update large amount of rows in PostgreSQL?

我只是一个虾纸丫 提交于 2019-12-11 03:33:34
问题 I need to update thousands of rows in a table. For example, I have 1000 rows with ids - 1, 2.. 1000: mytable: | id | value1 | value2 | | 1 | Null | Null | | 2 | Null | Null | ... | 1000 | Null | Null | Now I need to change first 10 rows. I can do it like this: UPDATE mytable SET value1=42, value2=111 WHERE id=1 ... UPDATE mytable SET value1=42, value2=111 WHERE id=10 This requires to many requests and not very fast, so I decide to do this optimization: UPDATE mytable SET value1=42 WHERE id in

Avoid multiple calls on same function when expanding composite result

陌路散爱 提交于 2019-12-11 02:54:39
问题 I have an SQL function retuning a composite result. CREATE TYPE result_t AS (a int, b int, c int, d numeric); CREATE OR REPLACE FUNCTION slow_function(int) RETURNS result_t AS $$ -- just some placeholder code to make it slow SELECT 0, 0, 0, ( SELECT sum(ln(i::numeric)) FROM generate_series(1, $1) i ) $$ LANGUAGE sql IMMUTABLE; When calling the function, I would like to have the parts of the composite type expanded into several columns. That works fine when I call: SELECT (slow_function(i)).*

How to use many LIKE operators and use index

寵の児 提交于 2019-12-11 02:54:10
问题 In my query I want find rows that match one of many LIKE operators. I know 3 ways of doing that but only one of them can use index. Lets start with table: CREATE TABLE dir ( id BIGSERIAL PRIMARY KEY, path TEXT NOT NULL ); CREATE INDEX path_idx ON dir(path TEXT_pattern_ops); After inserting sample data I can do: EXPLAIN ANALYZE SELECT id, path FROM dir WHERE path LIKE 'A%' OR path LIKE 'B%' OR path LIKE 'C%'; Above query use index correctly. Second way: EXPLAIN ANALYZE SELECT id, path FROM dir

Timeout expired, optimize query

牧云@^-^@ 提交于 2019-12-11 02:46:35
问题 I have this query to get all detail of Bills between two dates: SELECT DT.* FROM DetailTable DT, BillTable BT, PackageTable PT WHERE PT.Id = BT.IdPackage AND DT.IdBill= BT.Id AND PT.CodeCompany = @codeCompany AND PT.Date BETWEEN @begin and @end For every package there are many bills, and I want to get the details of bills of a company, the result in database it just 20,000 but I have : System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to

MySQL: Difference between `… ADD INDEX(a); … ADD INDEX(b);` and `… ADD INDEX(a,b);`?

谁都会走 提交于 2019-12-11 02:06:05
问题 Can someone tell me what is the difference between these two: ALTER TABLE x1 ADD INDEX(a); ALTER TABLE x1 ADD INDEX(b); AND ALTER TABLE x1 ADD INDEX(a,b); I know this goes down to the very basics but sometimes I find the former seems to be a little faster than the latter. Is it just my feeling or is there some actual reason for it? 回答1: With your second option, the following query will not use the index: SELECT * FROM x1 WHERE b = 'something'; The order in which columns are listed in the

Between statement missing indexes in certain cases

青春壹個敷衍的年華 提交于 2019-12-11 01:34:36
问题 I have a large database of numbers that I am looking for a match in between: For example: 1112203488 My table looks like this: | sATON | eATON | info | I have two index's on sATON and eATON (named s and e) So my SQL is as follows: SELECT * FROM `data2` FORCE INDEX ( s, e ) WHERE 1112203488 BETWEEN `sATON` AND `eATON`; So usually when the index is used the query will take nearly zero time (0.02). However, sometimes it looks like the table stats in MySQL is taking the decision to do a full

ORDER BY condition

走远了吗. 提交于 2019-12-11 01:16:44
问题 I would like to retrieve an ordered query result, but I need to have some specific row(s) to be in front of the list. Something like here on Stack Overflow, in the list of answers the right answer is always the first one. Assumed I need to have the rows with IDs 1,2,3 to be the head, the rest sorted by a date field, is it possible to do something like: SELECT * FROM foo ORDER BY id IN (1,2,3), created_date If not what is more efficient? There will be many rows! SELECT *, 0 AS head FROM foo