We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44231
    • 126 Posts
    hi there!
    I have this method of writing my queries,

    First:
    $path = MODX_CORE_PATH . 'components/mod1/';
    $result = $modx->addPackage('mod1',$path . 'model/','mod_');
    
    
    $output = null;
    $urlresult = "A";   
    
    /*for the A-Z drug search*/
    if(isset($_GET['alpha'])){
    if ((strlen($_GET['alpha'])!=null)||($_GET['alpha'])!=0){
    $urlresult = $_GET['alpha'];
    }
    }
    
    $query = $modx->newQuery('Tblbook');
            $query->select(array (
                'Tblbook.brand_name',
                'Tblbook.system_product_id',
                'Tblbook.system_brand_id',
                'TBOOKS.content',
                'TBOOKS.description',
                'TBOOKS.long_indication'
             
    
            ));
       
            $query->innerJoin('Tblbooks', 'TBOOKS', array(" Tblbook.system_brand_id = TBOOKS.system_brands_id")); 
            $query->where(array("Tblbook.brand_name:LIKE" => "$urlresult%"));
            $query->groupby('brand_name');
             
            $products = $modx->getCollection('Tblbook',$query); 
        
    



    Second:

    $search = $modx->query("SELECT b.form_class, a.system_brand_id, a.system_product_id, b.company, a.system_edition_id, a.brand_name, b.content, c.section1_id, c.section2_id, c.section3_id, c.section4_id,
    MATCH (tfv_records)AGAINST ('$xkeywordx' IN BOOLEAN MODE) AS relevance
    FROM mod_tfvrecords a, mod_tblcontents b, mod_tblproductcategory c
    WHERE MATCH (tfv_records)AGAINST ('$xkeywordx' IN BOOLEAN MODE)
    AND a.system_brand_id = b.system_brands_id
    AND a.system_product_id = c.system_product_id GROUP BY a.system_product_id HAVING relevance > 0.1 ORDER BY relevance DESC LIMIT 50");
    



    now my question is , what method is more optimize? what method is more convenient to use in modx?I having a hard time choosing with this method.

    can you give some advice? Im new in modx so I'm eager to learn new things in modx way smiley
    I hope you understand my problem!

    thanks!
      • 8386 ☆ A M B ☆
      • 160 Posts
      The second method is more optimised, since you are writing raw SQL code and don't require many method lookups. However the second method won't allow flexibility, in flexibility I mean abstraction to the database layer, If you were to try and run the second query against a MSSQL database it probably would fail and require a rewrite.

      The first method gives us the abstraction we need, so if we were to switch to MSSQL database there wouldn't be any need to rewrite the code. MODX will support more databases in the future, the first method mean you won't need to worry about your code that you write now to support databases in the future. The second method however will mean you have to rewrite your code if you need to support other database systems.

      I hope that helped answering your question.
        • 44231
        • 126 Posts
        thanks to responding

        hmm I'm really confuse why my query is slow sad

        I have tried this two method the second method give me much better results but not enough, I'm worried about when the page gets bigger many data will come from database maybe 2x of the size today or even much bigger, and yet the query gives me a slow execution.
          • 11055 ☆ A M B ☆
          • 3,112 Posts
          this might help
          https://forums.modx.com/thread/31992/full-text-searching-using-match-against-in-xpdo#dis-post-172948

          about the speed of the data return, indexing the field(s) helps.
            Rico
            Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
            MODx is great, but knowing how to use it well makes it perfect!

            www.virtudraft.com

            Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

            Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

            Maintainter/contributor of Babel

            Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.