How to use the NOEXPAND hint with Linq to SQL? - linq-to-sql

I have an indexed view that I need to specify the noexpand hint for in order for it to perform reasonably. Unfortunately as seen with regard to modifying the Linq to SQL generated T-SQL query from the NOLOCK hint it appears that there is no easy way to take advantage of these hints directly or is there?
My thought is that it would make sense to allow customization of this stuff through the use of attributes or declaratively through the dbml. Also since Linq to SQL seems to only work targeting SQL Server it only makes sense that we are also able to leverage these advanced features (if they exists). Regardless of the implementation though I'm interested in any creative ways of solving this problem.

I've found a workaround which appears to work but requires making a second View for each SQL View that one would want to use the NOEXPAND hint with. In the second view just select all of the fields from the original view and tack on the NOEXPAND hint. Any Linq to SQL query that needs to use the NOEXPAND hint can now just target that view that wraps the original.
More details can be found in this MSDN post.
One down side among others to consider when creating dependent views is that you will have to make sure to apply the creation scripts in the proper dependency order
Does anybody have any better alternatives? I would rather not have to create extra SQL Views just to support usage of this necessary optimizer hint.

I totally agree, but I do not believe there is such a method. In EF4 you can use the ExecuteStoreCommand that will let you execute SQL directly. That may be your only option if performance is unacceptable.
==EDIT==
You can also do this in LINQ to SQL through the ExecuteQuery method.
http://msdn.microsoft.com/en-us/library/bb399403.aspx

Related

Construct dynamic SQL queries in Go

I am not very experienced developer in backend, and I am trying to build the simple CMS. I use go and gin framework, and database/sql package to interact with DB. So, the question is that sometimes I have to perform very similar queries, but they are slightly different.
For example, sometimes I want to add where clause dynamically, or simply omit it. Or I have to create new row or updating existing – despite different query, they basically perform similar operations. Or I want to add or clause.
So, I write a lot of duplicated code. What is the best way to avoid it?

How do you test a SQL statements across multiple Database types?

Does anyone know of a quick and easy test to see if a query is properly formatted for both MySQL & MSSQL. Perhaps other database types as well, such as SQL Server? I only have access to MySQL at this time.
Info: I'm working on an Open Source project called JJWDesign Google Maps for SugarCRM. Some of the queries use the SugarCRM classes; others I have to write custom. For example, some are special distance calculations against the geocode information stored in the tables.
http://www.sugarforge.org/projects/jjwgooglemaps/
More importantly, while there is an accepted syntax, each flavour of database has it's own specific functions, features and things you can do.
The best you can do is to make do with the most basic of features. Oracle has different functions for datetime compared to mysql compared to db2. While I would love to assist in a 'free as in beer' project, you really will need to check each function to see if it is the same across all major vendors. General functions most often are, so abs() will be fairly consistent, but others simply won't.
You're talking about a SQL parser so by definition it either isn't going to be quick and easy or it will do only the simplest checking.
Each RDBMS has its own flavour of SQL too so you'd really be limited to testing whether it was ANSI SQL.

When to use "Linq to sql"?

What are the pro's and con's of using "Linq to SQL" and core ADO.NET technology for access databases?
Advantage
No need to create business objects dbml files will do for you
No need to worry about writing queries because linq2sql convert your statment in efficient queries
Important is Lazy Loading of related objects
Disadvantage
Disconnect linq is not supported i.e you cannot deatch you objects form DataContext object. for more detail : Most efficient way to update with LINQ to SQL
I have the same view point as this post, I've yet to find any major disadvantages of Linq.
I have built a number or application and websites using Linq and found it to be extremlly simple to use
http://forums.asp.net/t/1520157.aspx
comment by BoogleC
Regards
Sp
Id also be carefull about how you write you LINQ statement. Sometimes its better to compile your Linq rather than not as every single run of the Linq query is fully parsed every time it happens. See below linq
http://www.codinghorror.com/blog/2010/03/compiled-or-bust.html
I wouldn't recommend LINQ to SQL at all as it is effectively dead (you don't want to be writing legacy code, right?). Microsoft is no longer developing it and they recommend using the Entity Framework instead (see here), however, if you are interested in using an ORM, I would strongly recommend looking at NHibernate.

Is there a MySQL tool that generates SQL queries for you?

Is there any software for Windows that allows you to setup your tables and then tell the software what you want to SELECT given the conditions and it will tell you the SQL query that you need? Thanks.
Probably you'll find software that will do this for you in a GUI, but eventually you will be required to write the queries yourself, unless you're developing something really trivial.
A GUI tool for SELECT statements is a too-high level of abstraction for most scenarios, and you would have to face the "Law of Leaky Abstractions" eventually.
Visualization tools are quite handy for defining the database schema, and there are some very good tools for that. However queries remain quite tricky to visualize, in my opinion. I think you'll be able to mentally visualize queries, given enough practice. Nevertheless, I'm quite sure you will be able to get started doing SQL queries within a few days, even non-trivial ones.
In addition, you can use Stackoverflow if you require assistance with complicated queries. I'm quite sure that the community here will suggest better SQL than any GUI tool!
These sound like they will do what you want:
SQL Maestro
dbForge Query Builder for MySQL
Having said that, I do agree with the other answers here - learning to manually write SQL will give you a lot more control. I highly recommend the (free) MySQL Workbench for that.
The people at sqlmanager.net build some tools to deal with databases, but I agree with Daniel: if you have very complex queries to work with, it's probably more work telling the tool what to do that creating the queries manually - especially as you'll have to verify what the tool did, so if you can do that, you could have done it by hand in the first place :)
As said before by daniel vassallo, you should learn how to create those sql queries "by hand". where i work we use a program that does exactly this (it's used by accountants only) and it creates horrible sql select statements and we have constantly whining accountants that think that the problem is allways on the server side even when we show them that those queries get them the wrong data... it's a never ending circle :(
Use HeidiSQL to manage your database, and keep an eye on what's going on in the SQL log. You'll soon pick it up.

How is Linq-to-Sql abused?

I hear a lot of linq-to-sql bashing and how people will unknowingly abuse it. But how is linq-to-sql being abused?
Update
If someone can give me clear examples of how it's abused that would be very helpful. References to blogs/tutorials would be very helpful as well.
Thanks.
One of the easiest mistakes to make is to create a query which results in a loop of calls to the database instead of a single call returning all the data. For this reason it's worth checking what sql commands are hitting the database either in the debugger or with a trace.
There are many ways that LINQ->SQL can be abused just like poorly written inline SQL/ADO.NET/SPs or what have you.
Alot of what you might have heard is how LINQ itself might be abused.
I think one example of it being misused would be as a total replacement for SQL in views or stored procedures on the server, and therefore potentially more lax security on the database server.
Well... if you can write complicated queries with linq-to-sql it's probably because you know SQL syntax and are pretty good at writting the query in SQL in the first place. So why would you use the .NET syntax to write a syntax tree that then another layer of software would translate (perhaps not very efficiently) into SQL for you?
Just write the damn thing in SQL in the first place :-)