Thursday, 5 July 2012

Sphinx Search Optimization

Sphinx search is an alternative of performing search in mysql .
From my experience sphinx will provide you a great deal of performance .
For Details http://sphinxsearch.com

This Page is about the optimization that i had done and will boost your performance

* Caching (Use Memcache)
sphinx directly does not support caching. An alternative is to use memcache. 
Based on your website requirements , say i fetch records of 1st 10 pages  and store records in memcache. So when user uses pagination then there is no need to peform a search operation , rather we can fetch records directly from memcache

* Use Of Multiquery / faceted searching
Multi-queries, or query batches, let you send multiple queries to Sphinx in one go.Two API methods that implement multi-query mechanism are AddQuery() and RunQueries(). 


Why To Use:
1. decrease in network roundtrips. 
2. automatic internal optimizations.
3. for new version performance will always increase.


Sequential Code
$cl->SetGroupBy('grouping1', SPH_GROUPBY_ATTR,"@count desc");
$cl->AddQuery ('textToSearch_OPTINAL','IndexName');
$res=$cl->RunQueries();

$cl->SetGroupBy('grouping2', SPH_GROUPBY_ATTR,"@count desc");
$cl->AddQuery ('textToSearch_OPTINAL','IndexName');
$res=$cl->RunQueries();

$cl->SetGroupBy('grouping3', SPH_GROUPBY_ATTR,"@count desc");
$cl->AddQuery ('textToSearch_OPTINAL','IndexName');
$res=$cl->RunQueries();

can be optimized by Multiquery
$cl->SetGroupBy('grouping1', SPH_GROUPBY_ATTR,"@count desc");
$cl->SetGroupBy('grouping2', SPH_GROUPBY_ATTR,"@count desc");
$cl->SetGroupBy('grouping3', SPH_GROUPBY_ATTR,"@count desc");
$cl->AddQuery ('textToSearch_OPTINAL','IndexName');
$res=$cl->RunQueries();

* Avoid use of setSelect('limitedcolumns)
setSelect(*) is much faster than setSelect('limitedcolumns)
Reason:
You have a bucket of colored marbels.If I ask you to pass me all (the *), then you just pass me the bucket.But if I ask you to pass me only the red and yellow ones, it takes you time, because you
have to pick out the right ones, and put them in a new bucket.
Smaller bucket (ie less network bandwidth), but takes longer (processing)




* Use Of SphinxQL (for large in/not-in queries)

client end computations are very high specially when a lot of values are being sent
in a IN or NOT IN clause because for every value it converts it into some binary format
using pack function and then sends to the sphinx server. This causes high load on our web
server because we have queries wherein we show online users within a search criteria and
sometimes online users are around 15k-20k.

example:  normal use of api is slow and cpu utilization consuming

for($i=100;$i<20000;$i++) {        $x[]=$i; }

$cl->SetFilter('PROFILEID',$x,TRUE);
use of sphinxQL will give cpu utilization improvement by 10 times

Distributed searching
To scale well, Sphinx has distributed searching capabilities. Distributed searching is useful to improve query latency (ie. search time) and throughput (ie. max queries/sec) in multi-server, multi-CPU or multi-core environments. This is essential for applications which need to search through huge amounts data (ie. billions of records and terabytes of text).

* Explore sphinx.conf
1. mem_limit                       = 32M (default allocation)
change it to around 2048MB based on your usage.

View Documents online without signing up (Open Source):



  1. Google Docs Viewer:

    Google Docs is one of the most popular cloud based document editor and this is very easy tool to view documents online by using embedded code.
    Ex:  


    <iframe src=”http://docs.google.com/gview?url='Document_url'”

    style=”width:600px; height:400px;” frameborder=”1”>

    </iframe>

    OR

    <iframe src=”http://docs.google.com/viewer?url='Document_url'”
    style=”width:600px; height:400px;” frameborder=”1”>

    </iframe>
    Supported Formats: PDF, XLS, XLSX, DOC, DOCX, PPT, PPTX, PSD, TTF,                                     SVG, XPS

    RND Result: PDF, DOC, PPT, XLS, XLSX, TTF, XPS -Working

    DOCX, PPTX -Not working 


2.  Zoho Document Viewer:

  1. Zoho also a leader in providing web based documents and spreadsheets editing functionalities
    also has a very handy Zoho document viewer.

    This viewer is also same Google docs viewer. Zoho docs also gives an option for embedding

    the document via an iframe code.

    Ex: <iframe src="https://viewer.zoho.com/api/urlview.do?url='document_url'" style="width:600px; height:400px;" frameborder="1"></iframe>

    Supported Formats: Microsoft Office - doc, docx, xls, xlsx, ppt, pptx, pps;

    OpenDocument - odt, ods, odp; OpenOffice:sxw, sxc, sxi;

    Other Formats - wpd, pdf, rtf, txt, html, csv, tsv .

    RND Result: Still not working this API

    Error: Error occurred. Please try again.


3. View Docs online Viewer:

View Docs Online has a cool interface for viewing documents. The flash based interface

has almost all useful features like zooming, selecting text and viewing pages as tiles.

View Docs Offline also gives you an embed code for embedding to your website or blog.

Ex: <iframe src="http://www.viewdocsonline.com/embed/<'DocumentURL'>"

frameborder="0" width="600" height="500"> </iframe>


Supported Formats: Microsoft Office – DOC, DOCX, PPT, PPTX, PPS, XLS, XLSX.

PDF – PDF, PS.

Open office - ODT, ODP, SXI.

Text – TXT, RTF.



4.DocsPal Viewer:

DocsPal has an added feature of converting the documents from one format to another.

It has a very beautiful interface and you’ll be given two options at the beginning,

asking you to either convert or just view a document. Upload your file or give its URL and

view it on the fly. The file format converter is also very impressive and gives good results.

Comment: This viewer has excellent options for viewing and converting all

documents formats. And also given option for viewing

videos/audio/images.




5.Vuzit Viewer:

Web based(online) document viewer. Does't support embed url.



6. Samurajdata Viewer:

           This is a very simple document viewer which also allows viewing a document on the internet via its URL .

Open Source Api.
Supported Formats: Only PDF, Doc,Docx, PostScript.


  1. Scribd Viewer:
This is also Online document viewer.


8.  Box viewer:

This is also online document viewer with Embedded option.

Ex: <embed src="https://boxmarketing.box.net/embed/'DocumentURL'" width="466"
height="400" wmode="opaque" type="application/x-shockwave-flash"
allowFullScreen="true" allowScriptAccess="always" >

Supported Formats: Microsoft Office – DOC, DOCX, PPT, PPTX, PPS, XLS, XLSX.
PDF – PDF, PS.
Open office - ODT, ODP, SXI.
Text – TXT, RTF.


                                                                         -PAVANKUMAR JOSHI

Embedding All Online Videos on Your Web Page or Blog