Why we need LINQ? tho it's also generating query in background and executing in SQL, that we can do manually.
Any real time example would be appreciated.
For SQL, you don't particularly need LINQ. You could do all the same stuff it does if you wanted to. It just happens to be more convenient with LINQ in a couple of ways:
You end up with an IEnumerable that you don't have to care about the inner workings of. Just iterate over it much as you would a list or array. No code required to fetch rows, update cursors, etc.
From a consumer's point of view, that IEnumerable works the same as a query on objects, which is one of the places where LINQ really shines. You can replace one with the other, and the rest of the code need never know.
linq is not only related to database ,you can apply linq to list , array or any collection of data .....
Related
I want to request a high amount of records (100000 to 1000000) per select request with a join of three tables. Is the performance much better with nativeSQL instead of using spring-data-jpa for mapping it to #Entity objects?
Thx!
JPA and every ORM turn your query results into domain objects.
That of course takes resources. Spring Data JPA adds potential conversions to that and it preprocesses your query in order to support fancy ways of setting parameters.
If you are selecting large amounts of data the preprocessing of the statement probably doesn't matter that much.
But the conversion to domain objects will.
You used the word "migrating" which sounds like you are going to select data and then immediately write it somewhere else. If that is the case, use plain SQL and work directly on the ResultSet tell the driver to make it read only and forward only. See Understanding Forward Only ResultSet
I am pretty comfortable with basic MySQL commands, as well as complex joins, but not sure about this array or data type that I have come across.
The code looks like:
a:4:{i:9888;s:0:"";i:17148;s:0:"";i:9879;s:0:"";i:9881;s:0:"";}
a:1:{i:9857;s:0:"";}
a:0:{}
Can someone point me in the right direction of the following:
What is this called?
How can I do some basic commands without PHP to loop through? Such as get everything
where A is greater than 1?
Getting every row that includes 9857.
This is php serialized values: http://php.net/manual/en/function.serialize.php
AFAIK, you cannot directly deserialize it from mysql.
Additionally to the explanation what the string actually is, I'm telling you about an array in MySQL.
In fact, it doesn't exist. You could build a dirty kind of array by using dynamic columns but it's really, really dirty.
There is also SET and ENUM which translate a (predefined-) set of data into a bitmask. I am working on an online shop with several hundreds of tables we didn't use ENUM and SET once.
A proper way of implementing arrays in MySQL is a many-to-many or any "many" related relation between entities.
A Car having several Wheels (an array of wheels!)? Several Wheels attached to a single Car?
You would build two tables and connect them through a foreign key to implement an array. Tho this is not a "datatype" kind of array, but a "relation" array.
For example, say I'm creating a table which stores promo codes for a shipping site. I want to have the table match a promo code with the code's validator, e.g.
PROMO1: Order must have 3 items
PROMO2: Order subtotal must be greater than $50
I would like to store the query and/or routine in a column in the table, and be able to use the content to validate, in the sense of
SELECT * FROM Orders
WHERE Promo.ID = 2 AND Promo.Validation = True
Or something to that effect. Any ideas?
I wouldn't save the query in the database, there are far better possibilities.
You have to decide, which best fits your needs (it's not clear for me based on your question). You can use
Views
or
Prepared Statements
or
Stored Procedures
There's probably a better way to solve the issue, but the answer to your question is to write stored procedures that return the results you want. Where I work (and I hate this design), they actually store all queries and dml used by the application in stored procedures.
You can also dynamically build your queries using dynamic sql. For MySql, see the post below which might be of some help to you.
How To have Dynamic SQL in MySQL Stored Procedure
Otherwise, you can also store your queries in a string format in the database, and retrieve them and execute them using the EXECUTE statement, such as that post points out.
I'd personally keep away from designs like that though. Storing queries in XML isn't a bad alternative, and have your app be written to be extensible and configurable from XML, so you don't need to make code changes to add new validation logic, and instead just have to configure it in XML.
I'm developing a form generator, and wondering if it would be bad mojo to store JSON in an SQL database?
I want to keep my database & tables simple, so I was going to have
`pKey, formTitle, formJSON`
on a table, and then store
{["firstName":{"required":"true","type":"text"},"lastName":{"required":"true","type":"text"}}
in formJSON.
Any input is appreciated.
I use JSON extensively in my CMS (which hosts about 110 sites) and I find the speed of access data to be very fast. I was surprised that there wasn't more speed degradation. Every object in the CMS (Page, Layout, List, Topic, etc) has an NVARCHAR(MAX) column called JSONConfiguration. My ORM tool knows to look for that column and reconstitute it as an object if needed. Or, depending on the situation, I will just pass it to the client for jQuery or Ext JS to process.
As for readability / maintainability of my code, you might say it's improved because I now have classes that represent a lot of the JSON objects stored in the DB.
I used JSON.net for all serialization / deserialization. https://www.newtonsoft.com/json
I also use a single query to return meta-JSON with the actual data. As in the case of Ext JS, I have queries that return both the structure of the Ext JS object as well as the data the object will need. This cuts out one post back / SQL round trip.
I was also surprised at how fast the code was to parse a list of JSON objects and map them into a DataTable object that I then handed to a GridView.
The only downside I've seen to using JSON is indexing. If you have a property of the JSON you need to search, then you have to store it as a separate column.
There are JSON DB's out there that might server your needs better: CouchDB, MongoDB, and Cassandra.
A brilliant way to make an object database from sql server. I do this for all config objects and everything else that doesn't need any specific querying. extending your object - easy, just create a new property in your class and init with default value. Don't need a property any more? Just delete it in the class. Easy roll out, easy upgrade. Not suitable for all objects, but if you extract any prop you need to index on - keep using it. Very modern way of using sql server.
It will be slower than having the form defined in code, but one extra query shouldn't cause you much harm. (Just don't let 1 extra query become 10 extra queries!)
Edit: If you are selecting the row by formTitle instead of pKey (I would, because then your code will be more readable), put an index on formTitle
We have used a modified version of XML for exactly the purpose you decribe for seven or eight years and it works great. Our customers' form needs are so diverse that we could never keep up with a table/column approach. We are too far down the XML road to change very easily but I think JSON would work as well and maybe evan better.
Reporting is no problem with a couple of good parsing functions and I would defy anyone to find a significant difference in performance between our reporting/analytics and a table/column solution to this need.
I wouldn't recommend it.
If you ever want to do any reporting or query based on these values in the future it's going to make your life a lot harder than having a few extra tables/columns.
Why are you avoiding making new tables? I say if your application requires them go ahead and add them in... Also if someone has to go through your code/db later it's probably going to be harder for them to figure out what you had going on (depending on what kind of documentation you have).
You should be able to use SisoDb for this. http://sisodb.com
I think it not an optimal idea to store object data in a string in SQL. You have to do transformation outside of SQL in order to parse it. That presents a performance issue and you lose the leverage of using SQL native data parsing capability. A better way would be to store JSON as an XML datatype in SQL. This way, you kill two birds with one stone: You don't have to create shit load of tables and still get all the native querying benefits of SQL.
XML in SQL Server 2005? Better than JSON in Varchar?
i would like to return two record sets from a simple database table with one Linq2Sql query. I know how to do it if this was using Linq2Sql calling a stored procedure, but I don't want to use a stored procedure.
Is it possible to do it?
I've found an article here that has a suggested solution, but i hate the idea of having to write up a massive amount of code to partially extend the current context?! like... OUCH!!!
Just doesn't seem... right ?
Is the suggestion in the article the only way to do it? Are there other ways (without using stored procedures and still using Linq2Sql) ?
Wish Matt Warren was here to answer this :)
EDIT
I'm not asking about how to lazy-load / eager load (and using DataLoadOptions). That's a different concept.
Potentially you may do this with Multiple Active Result Sets (MARS) which I found from this page. It's an MSDN article on the topic, but does not specifically relate to LINQ to SQL, however this one does and probably the one you wanna check out.
Having said that, good luck because it looks like there's a bug posted to Microsoft regarding how it doesn't work, and the fix won't be here until .NET 4.0!
Lastly I understand you say you do not wish to use Stored Procedures, but if you do, I found a really simple guide here and here to get going.
That's the only way I've heard of it being done without a stored procedure. And you're right it does seem a bit excessive for a seemingly simple concept. If it was me I just get the records as separate result sets.