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!
Related
I need to compare two PostgreSQL databases with exactly one SQL-query. I tried with the following query:
SELECT *
FROM information_schema.columns
WHERE table_name IN (SELECT table_name
FROM information_schema.tables
WHERE table_schema NOT IN ('information_schema', 'pg_catalog')
AND table_type = 'BASE TABLE'
ORDER BY table_schema, TABLE_NAME);
It works for my problem but the foreign and primary keys are missing in this table. Is there are way to include them into my sql-query?
It is ok if the result table is not normalized and displays data multiple times. It is purely a runtime comparison, which is why the result tables are deleted again when the program has run through.
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
Using the psql client version 8.4.20, I created this select command that generates a bunch of other select commands:
mydatabase=# select concat('select count(*) from ', table_schema, '.', table_name, ';') from information_schema.tables where table_name like '%stockindex_alias%';
select count(*) from de_de.merged_stockindex_alias_de_de;
select count(*) from en_us.merged_stockindex_alias_en_us;
select count(*) from es_la.merged_stockindex_alias_es_la;
select count(*) from fr_fr.merged_stockindex_alias_fr_fr;
select count(*) from nl_nl.merged_stockindex_alias_nl_nl;
select count(*) from pt_br.merged_stockindex_alias_pt_br;
select count(*) from zh_hk.merged_stockindex_alias_zh_hk;
I know I can use \g to store those seven statements into a file, then execute the file with \i.
How can I execute the result of the query (those seven statements) in a single command without the intermediate file? I've tried \set, EXECUTE, searched the web, but can't get it right.
EDIT: The previous select statements erroneously had the word "table" in them, which I have fixed.
I don't know if it works in 8.4; it works in 9.2.
DO $$
DECLARE
x text;
BEGIN
FOR x IN (select concat('select count(*) from ', table_schema, '.', table_name, ';') from information_schema.tables where table_name like '%stockindex_alias%' LOOP
EXECUTE x;
END LOOP;
END;
$$;
Basically you create an anonymous function block and execute it. Function blocks (or whatever the correct name is) allow for variable declaration and dynamic execution.
If you can get by with an approximate count, you can use the system catalogs:
SELECT s.nspname AS locale, c.reltuples AS count
FROM pg_class c
JOIN pg_namespace s ON s.oid = c.relnamespace
WHERE c.relname LIKE '%stockindex_alias%';
This will be most accurate immediately after a VACUUM ANALYZE (assuming you are a superuser or own all the affected tables) and gradually reducing in accuracy as the affected tables are modified.
I've recently noticed some extra sql calls when using miniprofiler.
They only seem to occur after a build, and I think they appeared after upgrading to EF6.
Are they just checking for changes in models?
And can I safely ignore them?
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
) AS [GroupBy1]
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE ([Extent1].[ContextKey] = #p__linq__0) AND (#p__linq__0 IS NOT NULL)
) AS [GroupBy1]
SELECT TOP (1)
[Project1].[C1] AS [C1],
[Project1].[MigrationId] AS [MigrationId],
[Project1].[Model] AS [Model]
FROM ( SELECT
[Extent1].[MigrationId] AS [MigrationId],
[Extent1].[Model] AS [Model],
1 AS [C1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE ([Extent1].[ContextKey] = #p__linq__0) AND (#p__linq__0 IS NOT NULL)
) AS [Project1]
ORDER BY [Project1].[MigrationId] DESC
These DB calls refer to the new Migration HIstory Table feature included in EF6 when using Code-First:
Migrations history table is a table used by Code First Migrations to store details about migrations applied to the database. By default the name of the table in the database is __MigrationHistory and it is created when applying the first migration do the database. In Entity Framework 5 this table was a system table if the application used Microsoft Sql Server database. This has changed in Entity Framework 6 however and the migrations history table is no longer marked a system table.
If you aren't using them, these calls shouldn't cause any harm. You can always disable them or have EF create DB change scripts for you instead.
Say I have a query like this one:
var result=collection.OrderBy(orderingFunction).Skip(start).Take(length);
Will the whole query run on SQL server and return the result, or will it return the whole ordered table and then run Skip and Take in memory? I am concerned because I noticed that OrderBy returns IOrderedEnumerable.
How about something like this:
if(orderAscending)
orderedCollection=collection.OrderBy(orderingFunction);
else
orderedCollection=collection.OrderByDescending(orderingFunction);
var result=orderedCollection.Skip(start).Take(length);
Will the Skip and Take part run on Server or in memory in this case?
This query is translated into SQL. An Entity Framework query such as
myTable.OrderBy(row => row.Id).Skip(10).Take(20);
Will produce SQL resembling the following:
SELECT TOP (20) [Extent1].[Id] AS [Id]
FROM ( SELECT [Extent1].[Id], row_number() OVER (ORDER BY [Extent1].[Id] ASC) AS [row_number]
FROM [my_table] AS [Extent1]
) AS [Extent1]
WHERE [Extent1].[row_number] > 10
ORDER BY [Extent1].[Id] ASC
I recommend downloading LinqPad, a utility that allows you to execute EF queries (and other queries), and see the results and the corresponding SQL. It is an invaluable tool in developing high-quality EF queries.
Yes, it does translate to SQL. This is essential for paging.
You can verify this using SQL Profiler.