nhibernate with subquery on database [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I need to select a product for a user based on other data in the database.
If the data is filtered out on the database that will require less data to be send to the server.
User (Id)
Product (code)
Access (User_Id, code) // Matching users to object codes
Will this query execute on the database sending back the minimal amout of data?
var products = QueryOver.Of<Access>()
.Where(a => a.User_Id == User.Id())
.Select(Projections.Property<Acces>(a => a.Code));
var access = QueryOver.Of<Product>()
.WithSubquery.WhereProperty(h => h.Code)
.In(products)
.Future();

This is very reasonable way how to filter data. The result of your queries would look like one SELECT against the DB:
SELECT ...
FROM Product
WHERE Code IN (SELECT Code FROM Access WHERE UserId = #userId)
So, this will for sure be executed on the DB Server, less data will be transfered, and what's more, it also would allow you to do the correct paging (if needed) - this scenario is the way how to filter parent over its one-to-many relations (find Parents which child has...)
Maybe check these Join several queries to optimise QueryOver query, NHibernate QueryOver - Retrieve all, and mark the ones already "selected"

Related

Should Cartesian product stored in a single table or it should be split [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
We have a cartesian product and currently we are storing in a single table and this table is growing drastically and user can perform update and read operations on these entities and we have a soft delete when user deletes the record.
And now we are getting hit by performance issues for read and update operations.
Basically this cartesian product holds permissions for different roles and categories.
Wanted to know if anyone came across the similar situation and understand the best practices to store the cartesian product.
I was also thinking to split this into N table and then write and update individually for each type and then while reading perform a join.
So what are the best practices to store a cartesian product.
Cartesian product simply mean product of two tables, otherwise its just a table and not a Cartesian product, so of course it should be split and whenever needed, the Cartesian product can be produced from these 2 tables.
Imagine that Table1 and Table2 each has 1000 records, a Cartesian product of these 2, will have 1000000 records, the records that can simply be produced from these 2000 records.
And of course using conditions, you may produce only a portion of the Cartesian product that you need and not all of it.

Convert SQL query that uses CAST calls to LINQ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How would I convert this SQL query to Linq?
Declare #Date Date
select #Date = Max (InsertedDate) from Rates
SELECT * FROM Rates where CAST(InsertedDate AS Date) =CAST(#Date AS Date) ORDER BY InsertedDate DESC
Your Sql Query appears to find all records on the last inserted date. Your Sql is approximately equivalent to:
var lastDate = db.Rates.Max(r => r.InsertedDate).Date;
var allRatesOnTheLastDate = db.Rates.Where(r => r.InsertedDate >= lastDate)
.OrderByDescending(r => r.InsertedDate);
This should be more performant than your Sql, given that you are casting the InsertedDate column which will prevent SARGability of any index on the column.
(Assuming an EF or Linq2Sql context called db with a Rates entity bound to it)

Retrieve Complicated SQL Query from C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've a complicated SQL query. I have to retrieve that from C#. What is the best method to retrieve complicated SQL queries? (Like QueryByAttribute, FetchXML, QueryExpression etc.)
Here is my code:
SELECT R.Name
FROM role R
WHERE R.roleid IN
(SELECT roleid
FROM systemuserroles SR
WHERE SR.systemuserid IN
(SELECT S.systemuserid
FROM systemuser S
WHERE S.new_departmentid3 =
(SELECT new_departmentid3
FROM systemuser S
WHERE S.systemuserid = '8B8825F9-6B27-E411-8BA9-000C29E0C100')))
Thanks for the replies.
If you are using an on-premise system I would use the Filtered Views and create a SQL query against them directly. This is by far the best performing option and is fully supported.
If this isn't an option because you are using CRM Online then FetchXML will give you the best performance available.
I'm going to disagree with Ben on a few issues:
The Filtered View contains security checks within them, and are designed to be able to run reports for users (they are not technically supported, but I have had only one breaking change over the course of 12 rollups, and it was realtively minor). The Non-Filtered Views are nearly identical, except that they don't contain all of the extra joins to ensure the user has access to query the information they are attempting to. So in this aspect, the Non-Filtered Views are going to give you the best possible performance, but I would recommend only using it when it makes a big performance difference, and only for reports. (Theoretically you could go directly to the Tables, but this is seems much more likely to be changed by Microsoft with any given Roll Up).
The best possible performance for large data requests available for online is not Fetch-Xml, but actually Odata since the payload is much smaller with O-Data than the Fetch Xml. However there are some technical limitations to using oData (You wouldn't be able to do your current query in one call due to it having too many joins, but you could do it in two).
P.S. I think this is an easier to read equivalent SQL statement:
SELECT R.Name
FROM role R
INNER JOIN systemuserroles SR ON R.roleId = SR.roleId
INNER JOIN SystemUser S ON SR.systemuserid = S.systemuserid
INNER JOIN systemuser S2 ON S.new_departmentid3 = S2.new_departmentid3
WHERE S2.systemuserid = '8B8825F9-6B27-E411-8BA9-000C29E0C100'

How to connect to multiple database servers, display combined results? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have same table and same multiple database servers. How to connect to multiple database servers, obtain those records from each database server and then display the first 10 of the combined results ?
Say for instance you are querying the multiple instances using different connection strings for a sample table Orders, you could try the following:
var orders = ConfigurationManager.ConnectionStrings.Cast<ConnectionStringSettings>()
// filter to the relevant connection strings
.Where(s => s.ConnectionString.ToLower().Contains("metadata"))
.SelectMany(s => {
// for each connection string, select a data context
using(var context = new NorthwindEntities(s.ConnectionString)) {
// for each context, select all relevant orders
return context.Orders.ToArray();
} // and dispose of the context when done with it
})
.Take(10)
.ToList();
Here are a couple solutions off the top of my head.
Solution 1:
1 - Create a staging database / table on server A.
2 - Import all data from all servers into table.
3 - Query table to get results.
Solution 2:
1 - Create a Linked server for each server B .. Z on server A.
2 - Create Query using 4 part notations on linked servers.
Overall, solution 2 can be slow since you are using distributed transactions.
Solution 1 allows you to store the aggregated results that can be indexed (for speed) and can be queried multiple times.
As for importing the data from server to server, just pick a way to do it. There are two many solutions are there to get into the particulars.

Please Translate this from C# to SQL in plain english [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can someone please break this c# down in english? Is it possible to add a join in there somehow?
return db.Providers.Where(n => n.LastName.StartsWith(prefixText)).OrderBy(
n=>n.LastName).Select(n => n.LastName).Take(count).ToArray();
Thanks.
The SQL query is probably something like:
SELECT LastName FROM Providers
WHERE LastName LIKE 'PrefixText%'
ORDER BY LastName
LIMIT count; -- This may be TOP in MS SQL or ROWNUM in Oracle
Which means:
Give me all rows from the table Providers where the LastName column starts with PrefixText (whatever that variable contains). I want them sorted alphabetically by the LastName column, and I only want the first count rows (i.e., if count was equal to 50, you'd get up to 50 rows)
Sure, you can do a JOIN. You can refer to another table within your Where expression:
db.Providers.Where(n => n.ProviderGroup.ADgroup == 'Active Dir Group')
And the framework will automatically join in ADgroup for you, provided your model provides the necessary relationships between your tables.
Get x number of last name's of all Providers whose LastName starts with the text in the variable prefixText, in ascending alphabetical order.
This will return an array containing count last names, ordered in increasing alphabetical order, and starting with prefixText.
This is the corresponding SQL code:
select top #count LastName from Providers
where LastName like '+#prefixText+%'
order by LastName
Why do you need a join here?
Update:
Per the OP comment:
I need a join for limit the results of an Ajax auto extender...
You don't need a join for limiting the results in the Ajax Auto Extender, just use SQL's top clause or LINQ's Take method, just like you're doing right now:
db.Providers.Where(n => n.LastName.StartsWith(prefixText)).OrderBy(
n=>n.LastName).Select(n => n.LastName).Take(count).ToArray();

Categories