views:

238

answers:

4

Hi there,

The server scalability is a major concern for us. I am trying to do as much of the processing on the client as possible and since SQL server 2008 does not have native support for JSON but it does for XML...

I thought if I return my data (using FOR XML EXPLICIT), and then in C# (BRL Layer), I can just use one SqlDataReader call to read the entire XML and then pass it on to the client (JQuery).

Wouldn't this approach be faster & more scalable (on the server) than consuming the raw data in C# using a DataTable and returning it to the client via a webservice which serializes it into JSON?? Again the key point here is server scalability.

Thank you.

+3  A: 

If server scalability is an issue for you, why would you want the server to have to send a load of text down instead of the "raw" data in a native form that the driver will understand? If you want to ultimately serve the data as JSON, I don't see how adding an extra XML conversion at both the database and web service sides would help. If you can send it to the client in XML, are you sure you want the format read by the client to be that closely tied to the database? That doesn't sound like a good level of coupling to me. As soon as you need to make any changes to the format, you're suddenly back to parsing the XML, modifying it and then reserializing it.

The canonical advice about performance applies here, of course: when in doubt, measure. While I strongly suspect that using XML will make things marginally slower here rather than faster, I'd certainly recommend measuring it instead of guessing.

Are you even sure that this aspect of performance is an issue? I would take the non-XML route from a design viewpoint - if you pass the XML on to the client directly, the coupling level is too high, and if you don't then XML isn't providing any significant benefit.

As for scalability: I can't see that serialization would affect how the code scaled particularly. It would allow scaling both up (a more powerful server) and out (more servers of the same power) with no additional problems. It's not like it's introducing an extra "single master" which would cause bottlenecks. If anything, I'd expect the non-XML route to be more scalable by putting very slightly less load on the database server (which can prove a hard-to-scale bottleneck). That's based on a guess that it takes more effort to serialize the results to XML than using the native wire protocol data representations though - and I suspect the XML serialization in SQL server is pretty heavily optimised. Again, I'd urge you to measure the differences (and bear other factors in mind) before committing to one path or the other.

Jon Skeet
A: 

You would definitely consume more CPU cycles taking the C# approach than just outputing the straight XML from SQL server. There are other considerations here though. Is the SQL server on the web server? If not you have to consider the network bandwidth from SQL to the web server. A native stream from SQL to the web server is smaller than XML. This may prove more of a bottleneck than the extra CPU cycles. Also, understanding your are only talking about server performance, but json is a more compact stream to send the client and more readily processed giving the user a better experience.

Brian

Bernesto
A: 

Converting a SQL recordset to XML using FOR XML to a very expensive operation. When you look at the execution plan you'll see that the step in the plan which converts to data to XML using a very large percentage of the CPU power.

Its easier for the system to return the data in its native format and have the C# middle tier handle it from there.

mrdenny
A: 

You should avoid unnecessary transformations such as serializing to XML and then re-serializing as JSON.

If you are going to send JSON to the client, you should probably forget XML all together.

If you are going to use XML then you should remember:

Serializing to XML never is cheap when compared to simply not serializing.

Between, SQL Server and managed code, I would guess that SQL Server is better simply because it is highly optimized for this scenario and it does it in native code.

But that is not enough a reason to decide on serializing on SQL Server. You should profile CPU and memory usage on the database server and on the application server and delegate serialization to whichever tier is less loaded or for which it is easier to add a new machine to.

Alfred Myers
Oh no, I was never going to transform from XML into JSON. I thought if I can XML back more efficiently, I can just consume XML on the client.
Cyrus