What is the difference between using the LIMIT clause and using a fixed index to retrieve data from a query?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












This code recover the exact same account, I thought there could be differences with the way it ask the database ... Seems not to be the way it works,
So an idea ?



public static void TestSOQLLimitOrArrayIndex () 
List<Account> L1 = DataBase.query('SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1');

System.debug('Using LIMIT 1 --- Account : ' + a);


// avec [0]
Account a = [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0];
System.debug('Using [0] --- Account : ' + a);



Thanks !










share|improve this question























  • @renato Oliveira how did you make my code being colorfull while editing ? I tried, I used CTRL + K (shortcut) but ...
    – Alexis MASSON
    Sep 20 at 8:41






  • 1




    code isn’t colored while editing posts here. You have copy it from somewhere else and add four spaces per line to get it formatted on your text.
    – Renato Oliveira
    Sep 20 at 10:01










  • So you take care of adding 4 spaces at each of my code line ?
    – Alexis MASSON
    Sep 20 at 10:03
















up vote
4
down vote

favorite












This code recover the exact same account, I thought there could be differences with the way it ask the database ... Seems not to be the way it works,
So an idea ?



public static void TestSOQLLimitOrArrayIndex () 
List<Account> L1 = DataBase.query('SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1');

System.debug('Using LIMIT 1 --- Account : ' + a);


// avec [0]
Account a = [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0];
System.debug('Using [0] --- Account : ' + a);



Thanks !










share|improve this question























  • @renato Oliveira how did you make my code being colorfull while editing ? I tried, I used CTRL + K (shortcut) but ...
    – Alexis MASSON
    Sep 20 at 8:41






  • 1




    code isn’t colored while editing posts here. You have copy it from somewhere else and add four spaces per line to get it formatted on your text.
    – Renato Oliveira
    Sep 20 at 10:01










  • So you take care of adding 4 spaces at each of my code line ?
    – Alexis MASSON
    Sep 20 at 10:03












up vote
4
down vote

favorite









up vote
4
down vote

favorite











This code recover the exact same account, I thought there could be differences with the way it ask the database ... Seems not to be the way it works,
So an idea ?



public static void TestSOQLLimitOrArrayIndex () 
List<Account> L1 = DataBase.query('SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1');

System.debug('Using LIMIT 1 --- Account : ' + a);


// avec [0]
Account a = [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0];
System.debug('Using [0] --- Account : ' + a);



Thanks !










share|improve this question















This code recover the exact same account, I thought there could be differences with the way it ask the database ... Seems not to be the way it works,
So an idea ?



public static void TestSOQLLimitOrArrayIndex () 
List<Account> L1 = DataBase.query('SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1');

System.debug('Using LIMIT 1 --- Account : ' + a);


// avec [0]
Account a = [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0];
System.debug('Using [0] --- Account : ' + a);



Thanks !







apex soql list data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 20 at 0:46









Renato Oliveira

4,60911346




4,60911346










asked Sep 19 at 9:02









Alexis MASSON

1178




1178











  • @renato Oliveira how did you make my code being colorfull while editing ? I tried, I used CTRL + K (shortcut) but ...
    – Alexis MASSON
    Sep 20 at 8:41






  • 1




    code isn’t colored while editing posts here. You have copy it from somewhere else and add four spaces per line to get it formatted on your text.
    – Renato Oliveira
    Sep 20 at 10:01










  • So you take care of adding 4 spaces at each of my code line ?
    – Alexis MASSON
    Sep 20 at 10:03
















  • @renato Oliveira how did you make my code being colorfull while editing ? I tried, I used CTRL + K (shortcut) but ...
    – Alexis MASSON
    Sep 20 at 8:41






  • 1




    code isn’t colored while editing posts here. You have copy it from somewhere else and add four spaces per line to get it formatted on your text.
    – Renato Oliveira
    Sep 20 at 10:01










  • So you take care of adding 4 spaces at each of my code line ?
    – Alexis MASSON
    Sep 20 at 10:03















@renato Oliveira how did you make my code being colorfull while editing ? I tried, I used CTRL + K (shortcut) but ...
– Alexis MASSON
Sep 20 at 8:41




@renato Oliveira how did you make my code being colorfull while editing ? I tried, I used CTRL + K (shortcut) but ...
– Alexis MASSON
Sep 20 at 8:41




1




1




code isn’t colored while editing posts here. You have copy it from somewhere else and add four spaces per line to get it formatted on your text.
– Renato Oliveira
Sep 20 at 10:01




code isn’t colored while editing posts here. You have copy it from somewhere else and add four spaces per line to get it formatted on your text.
– Renato Oliveira
Sep 20 at 10:01












So you take care of adding 4 spaces at each of my code line ?
– Alexis MASSON
Sep 20 at 10:03




So you take care of adding 4 spaces at each of my code line ?
– Alexis MASSON
Sep 20 at 10:03










2 Answers
2






active

oldest

votes

















up vote
8
down vote













actually, there is a difference, in current example it impacts on total number of records retrieved by SOQL queries limits. Limits is one of the most important challenge working with salesforce platform. try to use them so less, as possible.




E.g. you have 50000 Accounts and you need to query only one, so that query [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0] will return all 50000 records, but you will get only one, that you need. In this case, not necessary limit will be hit. So if you know all restrictions to required record - apply them. In this case we can set, that only one record will be returned SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1. In such way only one record will be counted in total number of records retrieved by SOQL queries apex limit.




also there is difference between assigning SOQL result to List of records, or to Sobject



e.g. if no Accounts exists



List<Account> accts = [
select Id
from Account
limit 1
];
// no exception is thrown. accts.isEmpty() returns true

Account acct = [
select Id
from Account
limit 1
];
// exception is thrown System.QueryException: List has no rows for assignment to SObject





share|improve this answer






















  • Well, good point thanks !
    – Alexis MASSON
    Sep 19 at 9:21

















up vote
4
down vote













I'll just add to Alexander's answer.



As per the documentation for ORDER BY here



There is no guarantee of the order of results unless you use an ORDER BY clause in a query



So if you don't use ORDER BY, there can be a possibility you can get differently ordered of records each time you execute the query.



An ORDER BY clause would define the order in the SOQL rows returned. In that case, the first row returned by using LIMIT clause or just fetching the first record[0] will give the same record.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233064%2fwhat-is-the-difference-between-using-the-limit-clause-and-using-a-fixed-index-to%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    8
    down vote













    actually, there is a difference, in current example it impacts on total number of records retrieved by SOQL queries limits. Limits is one of the most important challenge working with salesforce platform. try to use them so less, as possible.




    E.g. you have 50000 Accounts and you need to query only one, so that query [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0] will return all 50000 records, but you will get only one, that you need. In this case, not necessary limit will be hit. So if you know all restrictions to required record - apply them. In this case we can set, that only one record will be returned SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1. In such way only one record will be counted in total number of records retrieved by SOQL queries apex limit.




    also there is difference between assigning SOQL result to List of records, or to Sobject



    e.g. if no Accounts exists



    List<Account> accts = [
    select Id
    from Account
    limit 1
    ];
    // no exception is thrown. accts.isEmpty() returns true

    Account acct = [
    select Id
    from Account
    limit 1
    ];
    // exception is thrown System.QueryException: List has no rows for assignment to SObject





    share|improve this answer






















    • Well, good point thanks !
      – Alexis MASSON
      Sep 19 at 9:21














    up vote
    8
    down vote













    actually, there is a difference, in current example it impacts on total number of records retrieved by SOQL queries limits. Limits is one of the most important challenge working with salesforce platform. try to use them so less, as possible.




    E.g. you have 50000 Accounts and you need to query only one, so that query [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0] will return all 50000 records, but you will get only one, that you need. In this case, not necessary limit will be hit. So if you know all restrictions to required record - apply them. In this case we can set, that only one record will be returned SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1. In such way only one record will be counted in total number of records retrieved by SOQL queries apex limit.




    also there is difference between assigning SOQL result to List of records, or to Sobject



    e.g. if no Accounts exists



    List<Account> accts = [
    select Id
    from Account
    limit 1
    ];
    // no exception is thrown. accts.isEmpty() returns true

    Account acct = [
    select Id
    from Account
    limit 1
    ];
    // exception is thrown System.QueryException: List has no rows for assignment to SObject





    share|improve this answer






















    • Well, good point thanks !
      – Alexis MASSON
      Sep 19 at 9:21












    up vote
    8
    down vote










    up vote
    8
    down vote









    actually, there is a difference, in current example it impacts on total number of records retrieved by SOQL queries limits. Limits is one of the most important challenge working with salesforce platform. try to use them so less, as possible.




    E.g. you have 50000 Accounts and you need to query only one, so that query [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0] will return all 50000 records, but you will get only one, that you need. In this case, not necessary limit will be hit. So if you know all restrictions to required record - apply them. In this case we can set, that only one record will be returned SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1. In such way only one record will be counted in total number of records retrieved by SOQL queries apex limit.




    also there is difference between assigning SOQL result to List of records, or to Sobject



    e.g. if no Accounts exists



    List<Account> accts = [
    select Id
    from Account
    limit 1
    ];
    // no exception is thrown. accts.isEmpty() returns true

    Account acct = [
    select Id
    from Account
    limit 1
    ];
    // exception is thrown System.QueryException: List has no rows for assignment to SObject





    share|improve this answer














    actually, there is a difference, in current example it impacts on total number of records retrieved by SOQL queries limits. Limits is one of the most important challenge working with salesforce platform. try to use them so less, as possible.




    E.g. you have 50000 Accounts and you need to query only one, so that query [SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account][0] will return all 50000 records, but you will get only one, that you need. In this case, not necessary limit will be hit. So if you know all restrictions to required record - apply them. In this case we can set, that only one record will be returned SELECT Id, Name, Type, ParentId, Fax, Phone FROM Account LIMIT 1. In such way only one record will be counted in total number of records retrieved by SOQL queries apex limit.




    also there is difference between assigning SOQL result to List of records, or to Sobject



    e.g. if no Accounts exists



    List<Account> accts = [
    select Id
    from Account
    limit 1
    ];
    // no exception is thrown. accts.isEmpty() returns true

    Account acct = [
    select Id
    from Account
    limit 1
    ];
    // exception is thrown System.QueryException: List has no rows for assignment to SObject






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 19 at 9:16

























    answered Sep 19 at 9:10









    Oleksandr Berehovskiy

    6,58111532




    6,58111532











    • Well, good point thanks !
      – Alexis MASSON
      Sep 19 at 9:21
















    • Well, good point thanks !
      – Alexis MASSON
      Sep 19 at 9:21















    Well, good point thanks !
    – Alexis MASSON
    Sep 19 at 9:21




    Well, good point thanks !
    – Alexis MASSON
    Sep 19 at 9:21












    up vote
    4
    down vote













    I'll just add to Alexander's answer.



    As per the documentation for ORDER BY here



    There is no guarantee of the order of results unless you use an ORDER BY clause in a query



    So if you don't use ORDER BY, there can be a possibility you can get differently ordered of records each time you execute the query.



    An ORDER BY clause would define the order in the SOQL rows returned. In that case, the first row returned by using LIMIT clause or just fetching the first record[0] will give the same record.






    share|improve this answer
























      up vote
      4
      down vote













      I'll just add to Alexander's answer.



      As per the documentation for ORDER BY here



      There is no guarantee of the order of results unless you use an ORDER BY clause in a query



      So if you don't use ORDER BY, there can be a possibility you can get differently ordered of records each time you execute the query.



      An ORDER BY clause would define the order in the SOQL rows returned. In that case, the first row returned by using LIMIT clause or just fetching the first record[0] will give the same record.






      share|improve this answer






















        up vote
        4
        down vote










        up vote
        4
        down vote









        I'll just add to Alexander's answer.



        As per the documentation for ORDER BY here



        There is no guarantee of the order of results unless you use an ORDER BY clause in a query



        So if you don't use ORDER BY, there can be a possibility you can get differently ordered of records each time you execute the query.



        An ORDER BY clause would define the order in the SOQL rows returned. In that case, the first row returned by using LIMIT clause or just fetching the first record[0] will give the same record.






        share|improve this answer












        I'll just add to Alexander's answer.



        As per the documentation for ORDER BY here



        There is no guarantee of the order of results unless you use an ORDER BY clause in a query



        So if you don't use ORDER BY, there can be a possibility you can get differently ordered of records each time you execute the query.



        An ORDER BY clause would define the order in the SOQL rows returned. In that case, the first row returned by using LIMIT clause or just fetching the first record[0] will give the same record.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 19 at 9:17









        Hemant Jain

        1,607315




        1,607315



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233064%2fwhat-is-the-difference-between-using-the-limit-clause-and-using-a-fixed-index-to%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay