Q:

Optimization: Need to cut-down on server load

Our predicament is that we are using 3 media queries with different layouts which means that we're essentially generating all of the content 3 times. For instance:
<div class="small-media">
  {embed="template/view_small"}
</div>

<div class="medium-media">
  {embed="template/view_medium"}
</div>

<div class="large-media">
  {embed="template/view_large"}
</div>

You can see how ridiculous this is. We have tons of things being pulled in and we're starting to see overages on our data for doing it this way.
We're wondering if there's a way to use PHP (or EE itself) at the top of the document to load everything once and then send only the pertinent data to the nested templates at each media query.
Thanks for any input on this.

A:

What you're doing isn't really the way to use media queries.
Why is the 'data' (from which I read 'content') different based on the user agent? Is it actually different content or are you simply talking about layout?
I would say your options here are;

Output everything once and use CSS Media Queries properly to reformat the content appropriately for the device being used to access it. i.e Make the site responsive. There should be few, if any, instances where you'd need to duplicate output data like this.
Media queries should be used to alter how elements are displayed and interact with other elements, adjust page flow or show/hide elements from view. The underlying page markup and content should be identical regardless of the user agent properties.
There are numerous articles and tutorials around regarding this approach. Try http://mobile.smashingmagazine.com/2010/07/19/how-to-use-css3-media-queries-to-create-a-mobile-version-of-your-website/ to start with and descend down the rabbit-hole from there!
Use separate templates (or even domains) for each 'version' of the site and sniff the device when someone lands on a page, then redirect to the appropriate version. The disadvantage of this is multiple URLs for what is essentially the same content so be sure to specify canonical URLs for each page.
You could use PHP to sniff device properties and use a PHP conditional to add the template partial as required. There are a number of libraries available for doing this. I can't recommend any though since I haven't used them. 
Try this library as a starting point https://code.google.com/p/php-mobile-detect/
Use an add-on to do the detection. Try http://devot-ee.com/add-ons/detect-mobile. I don't know whether this will ease the load as you may find that EE's parsing engine will still do more than it needs to. It depends when the add-on fits in the parse order.
Use Stash. Someone else mentioned this so I won't go into it further here.

Personally I'd go with option 1.

