I’m trying to design a page to make it work the fastest while also trying to avoid duplication.
Basically, I’m going to load 3 sets of a user’s contacts. Business contacts, personal contacts, and special contacts, which could be a mixture of both business/personal. So in essence I’d be doing these 3 queries:
SELECT stuff FROM contacts WHERE userId = '$userId' AND type = 'business'
SELECT stuff FROM contacts WHERE userId = '$userId' AND type = 'personal'
SELECT stuff FROM contacts WHERE userId = '$userId' and isSpecial = 1
These contacts will be used by javascript to populate certain <div>s on the page and be used in searching the contacts on the page.
Would it make more sense to load only the business and personal contacts, then loop through them via javascript and use javascript to build the 3rd list of special contacts, essentially where isSpecial is set to 1?
Or would it be better to get the 3 sets seperately using a MySQL query and pass them on to javascript as 3 seperate sets from the get go?
Or should I only get 1 set of contacts from MySQL as long as userId = '$currentUser' and sort them into seperate lists via javascript when the page loads? (I will be looping through all contacts on the page load anyway to populate their respective <div>s, perhaps I could also sort them into 3 seperate lists while I’m looping through them?)
Or would any other approach be the best?
My concern here is to make everything as fast as possible for the user, while also minimizing the strain on the database server.
IMO it’s be better if you use MySQL itself to get the contacts separately as MySQL is comparatively faster. Also just in case if javascript is turned off in the users browser, or if he is using mobile browser, then you’ll not be able to segregate the contacts.
EDIT
As you has pointed out, javascript is a minimum requisite for your site, then the best way would be to use a single go with a single query to get the data and segregate using js.