php - How to optimize jquery chosen plugin for loading more than 50k results -
i using class 'chosen-select' loading product names.when user clicks on product_select , takes lot of time load.what can done improve performance.
<select class='chosen-select' data-placeholder="choose product..." id="product_select"> <option></option> <?php include 'connection.php'; $query = "select * " . $table_name; $result = mysqli_query($con, $query); $results = array(); while ($line = mysqli_fetch_assoc($result)) { $results[] = $line; } $i = 0; foreach ($results $r) { $i++; echo "<option value=" . $r['id'] . ">" . $r['product_name'] . "</option>"; } ?> </select> <script> $('#product_select').chosen({max_selected_options: 1}); </script>
what should do
it's not idea serving many options in 1 go, sanity of users. if me i'd switch auto-complete model or way user narrow option request down subset before requesting, however, here tips on can use optimise.
what can do
the real point can optimise — without rewriting jquery plugin — improve things @ server-side. include (as stated samuel liew) caching servers response, improving connection database i.e. make sure dealing local database rather remote. simple steps can towards faster queries:
by specifying target fields rather using
*
may large number of rows.
15 tips on optimising mysql queries
even changing type of database/table can — example, if hardware has low disk write speed/efficiency try running table memory. these kinds of options require must have lot of control on database server , database config, if using shared host unlikely can of anything... may have choice between using mysql or sqlite though, enough switch between disk-based database more memory-based one. if running dedicated server there whole host of things can optimise mysql, takes lot of reading (it's complicated subject), thread hints @ of things can mysql:
optimal mysql-configuration (my.cnf)
one thing may help, depend on how memory database instance has, build html in query. wouldn't advise complicated queries, generating quite simple. following assumes mysql usage:
select group_concat( '<option value="',id,'">', name, '</option>' separator '' ) html $table_name group null
once you've got database bringing html want php, either cache result in db table, or file in local filesystem; on next request option list should serve cached location instead — , continue until prefixed expiry time (or cache deleted).
the following link explains rough process, needs write result brought database; rather ob_get_contents()
in example — 1 way server-side cache however.
easy server-side caching in php
client-side caching
depending on how displaying input or how users might encounter it, optimise on client-side (for browsers support it) using window.localstorage
object. allow put check of localstorage before making ajax select request, if have options html stored there can use instead , cut out request entirely. there seems unofficial storage limit of 5m localstorage
should cover 50000 items. wouldn't speed things user's first visit, subsequent visits should improved quite bit. should bear in mind way cache cleared if database table updated.
Comments
Post a Comment