What is the difference between using the LIMIT clause and using a fixed index to retrieve data from a query?
Clash 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 !
apex soql list data
add a comment |Â
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 !
apex soql list data
@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
add a comment |Â
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 !
apex soql list data
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
apex soql list data
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
add a comment |Â
@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
add a comment |Â
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
Well, good point thanks !
â Alexis MASSON
Sep 19 at 9:21
add a comment |Â
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.
add a comment |Â
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
Well, good point thanks !
â Alexis MASSON
Sep 19 at 9:21
add a comment |Â
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
Well, good point thanks !
â Alexis MASSON
Sep 19 at 9:21
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Sep 19 at 9:17
Hemant Jain
1,607315
1,607315
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
@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