should i use non cluster index - entity-framework

I am working on performance tuning of Linq to SQL statement.
I tried transaction scope with read uncommitted. intesion was to use NOLOCK alternate. Didn't give me good result
Query i am tuning has 7 tables with count as below
select count(1) from [Extent1] --3211160
select count(1) from [Extent2] -- 2357380
select count(1) from [Extent3] -- 14118987
select count(1) from [Extent4] -- 14118639
select count(1) from [Extent5] -- 30196545
select count(1) from [Extent6] -- 14118883
select count(1) from [Extent7] --53066591
Extent1.CreatedOn sorting is consuming 97% of Execution plan.
Does using non cluster index on CreatedOn column help any improvement?
Any suggestion with better approach to solve this kind of issue also appriciated.
Thanks

Related

function to_number(bigint) does not exists

How can we convert the below oracle query into Postgres?
SELECT EMPLOYEE_CD AS EMPLOYEE_CD,
EMPLOYEE_ID AS EMPLOYEE_ID,
TO_NUMBER(COUNT (1)) AS CNT
FROM EMPLOYEE
GROUP BY EMPLOYEE_CD, EMPLOYEE_ID
HAVING COUNT (1) > 1;
It makes no sense to convert a number to a number (and this senseless in Oracle as well). So just remove the to_number() from your query:
SELECT EMPLOYEE_CD AS EMPLOYEE_CD,
EMPLOYEE_ID AS EMPLOYEE_ID,
COUNT (*) AS CNT
FROM EMPLOYEE
GROUP BY EMPLOYEE_CD, EMPLOYEE_ID
HAVING COUNT (*) > 1;
It has been a long standing, but nevertheless wrong, myth that count(1) is faster than count(*). In Postgres count(1) is actually slower than count(*). So using count(1) never makes sense nor does it improve anything.

query optimization how to reduce planning time and execution time in postgresql

query optimization how to reduce planning time and execution time in postgresql.
SQL Query :-
select s.id,state_name state, d.id no_districts,b.id no_blocks,v.id no_villages,district_name district,block_tehsil_name block,village_name village ,corr_vs,non_corr_vs,corr_gw,non_corr_gw,corr_sw,non_corr_sw
from ref_state s left join ref_district d on d.ref_state_id=s.id
left join ref_block_tehsil b on d.id=b.ref_district_id
left join ref_village v on v.ref_block_tehsil_id=b.id
left join (select ref_village_id ,coalesce(sum(tot_vs),0)-coalesce(sum(non_corr_vs),0) corr_vs,sum(non_corr_vs)non_corr_vs
from (select ref_village_id,count(vs.id)tot_vs,(select count(id) from mi_census_village_schedule_validation vsv where vsv.ref_village_id=vs.ref_village_id)non_corr_vs from mi_census_village_schedule vs group by ref_village_id )vs1 group by ref_village_id ) vs on vs.ref_village_id=v.id
left join(select ref_village_id ,coalesce(sum(tot_gw),0)- coalesce(sum(non_corr_gw),0) corr_gw,sum(non_corr_gw)non_corr_gw from (select ref_village_id,count(gws.id)tot_gw,(select count(id) from mi_census_ground_water_scheme_validation gwsv where gwsv.ref_village_id=gws.ref_village_id)non_corr_gw from mi_census_ground_water_scheme gws group by ref_village_id )gws1 group by ref_village_id ) gws on gws.ref_village_id=v.id
left join (select ref_village_id,coalesce(sum(tot_sw),0)- coalesce(sum(non_corr_sw),0) corr_sw,sum(non_corr_sw)non_corr_sw from(select ref_village_id,count(sws.id)tot_sw,(select count(id) from mi_census_surface_water_scheme_validation swsv where swsv.ref_village_id=sws.ref_village_id)non_corr_sw from mi_census_surface_water_scheme sws group by ref_village_id )sws1 group by ref_village_id ) sws on sws.ref_village_id=v.id
where s.id=30and d.id=21 and b.id=127 and v.id=632
there are several techniques that can help improve the performance of SQL queries under workspaces. Follow the SQL best practices to ensure query optimization like
proper indexes,so that SQL queries can cause minimal table scans
Avoid using functions in predicates.
Avoid using wildcard (%) at the beginning of a predicate.
Avoid unnecessary columns in SELECT clause.
Use inner join, instead of outer join if possible. -- You have used multiple left join in your above query which also impact your query.
DISTINCT and UNION should be used only if it is necessary.
use of order by clause when sorted result set is required.Be aware of the performance impact of adding the ORDER BY clause, as the database needs to sort the result set, resulting in one of the most expensive operations in SQL execution

Multiple Selection in Datagrip (Running a CTE that's not in your nested query)

I'm looking for a way to run a nested correlated query that requires a CTE created above the script. For example if I had:
with first_cte as (
select *
from a_table
where 1=1
)
select * from
(select
column_1,
column_2,
column_3
from b_table b
inner join first_cte f on f.user_id = b.user_id
where 1=1) x
If I just wanted to test the nested query, it will say that first_cte doesn't exist. Is there a way to highlight the CTE so that it will run when I'm testing nested queries?
I'm using PostgreSQL btw. Thanks!!!
There are Execute selection and Execution options features in DataGrip.

Does SQL execute subqueries fully?

Imagine I have this SQL query and the table2 is HUGE.
select product_id, count(product_id)
from table1
where table2_ptr_id in (select id
from table2
where author is not null)
Will SQL first execute the subquery and load all the table2 into memory? like if table1 has 10 rows and table2 has 10 million rows will it be better to join first and then filter? Or DB is smart enough to optimize this query as it is written.
You have to EXPLAIN the query to know what it is doing.
However, your query will likely perform better in PostgreSQL if you rewrite it to
SELECT product_id
FROM table1
WHERE EXISTS (SELECT 1
FROM table2
WHERE table2.id = table1.table2_ptr_id
AND table2.author IS NOT NULL);
Then PostgreSQL can use an anti-join, which will probably perform much better with a huge table2.
Remark: the count in your query doesn't make any sense to me.

LinqPad queries __MigrationHistory on first run of an EF query

My project is EF 5, using DbContext.
I just noticed that the first time I run any Linq query in LinqPad, there is a slight delay, and the generated SQL starts with the following. The subsequent runs, there is no delay and no extra SQL.
Can anyone explain to me what this SQL is, and if I should worry about it?
SELECT TABLE_SCHEMA SchemaName, TABLE_NAME Name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'
GO
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
) AS [GroupBy1]
GO
SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC
GO
That's EF code first, verifying that your database matches the model to be sure everything will work properly.
Don't worry about it!