query-optimization

Same query - different execution plans

风流意气都作罢 提交于 2020-01-01 08:53:30
问题 SQL 2008. I have a test table: create table Sale ( SaleId int identity(1, 1) constraint PK_Sale primary key, Test1 varchar(10) null, RowVersion rowversion not null constraint UQ_Sale_RowVersion unique ) I populate it with 10k test rows. declare @RowCount int = 10000 while(@RowCount > 0) begin insert Sale default values set @RowCount -= 1 end I run these two queries: -- Query #1 select * from Sale where RowVersion > 0x000000000001C310 -- Query #2 declare @LastVersion rowversion =

Same query - different execution plans

假如想象 提交于 2020-01-01 08:53:26
问题 SQL 2008. I have a test table: create table Sale ( SaleId int identity(1, 1) constraint PK_Sale primary key, Test1 varchar(10) null, RowVersion rowversion not null constraint UQ_Sale_RowVersion unique ) I populate it with 10k test rows. declare @RowCount int = 10000 while(@RowCount > 0) begin insert Sale default values set @RowCount -= 1 end I run these two queries: -- Query #1 select * from Sale where RowVersion > 0x000000000001C310 -- Query #2 declare @LastVersion rowversion =

Does the order of columns in a query matter?

拥有回忆 提交于 2020-01-01 08:44:14
问题 When selecting columns from a MySQL table, is performance affected by the order that you select the columns as compared to their order in the table (not considering indexes that may cover the columns)? For example, you have a table with rows uid, name, bday, and you have the following query. SELECT uid, name, bday FROM table Does MySQL see the following query any differently and thus cause any sort of performance hit? SELECT uid, bday, name FROM table 回答1: The order doesn't matter, actually,

Linq2Sql: query - subquery optimisation

元气小坏坏 提交于 2019-12-31 06:17:04
问题 I have the following query: IList<InfrStadium> stadiums = (from sector in DbContext.sectors where sector.Type=typeValue select new InfrStadium(sector.TeamId) ).ToList(); and InfrStadium class constructor: private InfrStadium(int teamId) { IList<Sector> teamSectors = (from sector in DbContext.sectors where sector.TeamId==teamId select sector) .ToList<>(); ... work with data } Current implementation perform 1+n queries, where n - number of records fetched the 1st time. I want to optimize that.

Which column to put first in index? Higher or lower cardinality?

删除回忆录丶 提交于 2019-12-31 02:51:09
问题 For example, if I have a table with a city and a state column, what is the best way to use the index? Obviously city will have the highest cardinality, so should I put that column first in the index, should I put state or doesn't it matter much? 回答1: MySQL composite index lookups must take place in the order in which the columns are defined within the index. Since you want MySQL to be able to discriminate between records by performing as few comparisons as possible, with all other things

Forcing a SQL Remote Query to filter remotely instead of locally

江枫思渺然 提交于 2019-12-31 02:22:05
问题 I have a MS SQL Query that is pulling data via from a remote server. The data that I'm pulling down needs to be filtered by a date that is determined at run time.. When I run the query like this: SELECT * FROM SERVER.Database.dbo.RemoteView WHERE EntryDate > '1/1/2009' then the filter is applied remotely... However, I don't actually want to use '1/1/2009' as the date - I want the date to be supplied by a user-defined function, like this: SELECT * FROM SERVER.Database.dbo.RemoteView WHERE

MySQL EXPLAIN 'type' changes from 'range' to 'ref' when the date in the where statement is changed?

牧云@^-^@ 提交于 2019-12-30 20:29:28
问题 I've been testing out different ideas for optimizing some of the tables we have in our system at work. Today I came across a table that tracks every view on each vehicle in our system. Create table below. SHOW CREATE TABLE vehicle_view_tracking; CREATE TABLE `vehicle_view_tracking` ( `vehicle_view_tracking_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `public_key` varchar(45) NOT NULL, `vehicle_id` int(10) unsigned NOT NULL, `landing_url` longtext NOT NULL, `landing_port` int(11) NOT NULL,

MySQL EXPLAIN 'type' changes from 'range' to 'ref' when the date in the where statement is changed?

时光总嘲笑我的痴心妄想 提交于 2019-12-30 20:28:57
问题 I've been testing out different ideas for optimizing some of the tables we have in our system at work. Today I came across a table that tracks every view on each vehicle in our system. Create table below. SHOW CREATE TABLE vehicle_view_tracking; CREATE TABLE `vehicle_view_tracking` ( `vehicle_view_tracking_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `public_key` varchar(45) NOT NULL, `vehicle_id` int(10) unsigned NOT NULL, `landing_url` longtext NOT NULL, `landing_port` int(11) NOT NULL,

How to Improve Query Performance with many JOINs

扶醉桌前 提交于 2019-12-30 00:54:30
问题 I have a query (with the purpose of making a view) which is using a few joins to get each column. Performance degrades quickly (exponentially?) for each set of joins added. What would be a good approach to make this query faster? Please see comments within the query. If it helps, this is using the WordPress DB schema. Here is a screenshot of EXPLAIN PRODUCTS TABLE +--+----+ |id|name| +--+----+ |1 |test| +--+----+ METADATA TABLE +----------+--------+-----+ |product_id|meta_key|value| +--------

Select non-empty columns using SQL Server

筅森魡賤 提交于 2019-12-29 08:24:09
问题 I am using SQL Server 2012. i have a table with 90 columns. I am trying to select only columns that contains data. After searching i used the following procedure: 1- Getting all columns count using one select query 2- Pivoting Result Table into a Temp table 3- Creating Select query 4- Executing this query Here is the query i used: DECLARE @strTablename varchar(100) = 'dbo.MyTable' DECLARE @strQuery varchar(max) = '' DECLARE @strSecondQuery varchar(max) = 'SELECT ' DECLARE @strUnPivot as