Simple For each – base table and order clause

Simple For each – base table and order clause
To determine the base table GeneXus will extract the attributes referred in the For each…
… then finds the tables where they are located:
Video recorded with GeneXus X Evolution 2
Page
What will happen if we add, in the For each, an attribute of Attraction, such as
AttractionName?
1
… and takes the base table whose extended table contains them:
For example, in the printblock:
Page
And what does this mean? That the listing will not contain countries, but rather tourist
attractions… and the country and city listed in each case will be those of the attraction and not
those of the CountryCity table.
2
Now the tables involved are Attraction, CountryCity and Country. So the base table will now be
Attraction:
Video recorded with GeneXus X Evolution 2
{Demo}
So, when we execute…
Page
3
we can see the listing that results:
Video recorded with GeneXus X Evolution 2
Where the navigation listing tells us that the base table is Attraction…
…and that it will be gone through in an order based on AttractionId, primary key…
…going through the whole table…
Page
4
…and accessing to Country (to recover CountryName, the country of the attraction) as well as
to CountryCity to recover CityName (provided CityId has a value – since we had specified the
possibility of leaving the value of that attribute as null).
Video recorded with GeneXus X Evolution 2
We would obtain the same base table (Attraction) if instead of adding the AttractionName
attribute to the printblock we added it, for example, in the order clause.
Now we require that it go over the base table of the For each, sorted by AttractionName.
Again, in order to determine the base table, GeneXus will extract all the attributes of the For
each, including those of the order and where clauses, if any, etc.
{Demo}
Page
5
Let’s see the navigation listing… and the listing that results
Video recorded with GeneXus X Evolution 2
The base table is Attraction. It will sort by AttractionName, and for each record, it will access
the Country table through the value of the foreign key, CountryId, to recover the value of
CountryName.
The same will happen in the case of CountryCity to recover the value of CityName.
In the pdf we can now see that only the country and the city of each attraction are printed:
Video recorded with GeneXus X Evolution 2
Page
The navigation listing also informs us that there is no index in the database by the attribute by
which we need to sort the information, so our performance could be low for this query. Why?
6
To verify this we can add the name of the attraction in the listing…
Let’s suppose the Attraction table with the following data…
If we need to obtain its records sorted by the attribute AttractionName, then it will
have to be reordered by this attribute in some way. However, this could take too long
if millions of records are involved.
The existence of an index at the physical table level would optimize the query.
We should recall that indices are efficient ways to access data, just like dictionaries that index
by a specific attribute or set of attributes. In this case one: AttractionName.
The disadvantage in creating an index is that, from there on, it has to be maintained. So, when
an attraction is added, such as the Sao Paulo obelisk, then the index must be reorganized.
Creating an index for a table in the database is simple and may be done anytime…
{Demo}
We find the table, open it and go to the section that includes information about the indices
defined. These three indices are the ones automatically created by GeneXus from the keys:
primary and foreign, to make referential integrity controls efficient.
Page
7
Let’s create a user index. When we press enter, we get this default name, which we can
change as we please…
Video recorded with GeneXus X Evolution 2
Our index consists of the AttractionName attribute sorted in ascending order.
If attraction names cannot be duplicated, we must control this by indicating that the index
must be Unique and Not Duplicate. In such case, an automatic control will be done every time
that an attraction is entered so that there is no other attraction with the same name.
In our case repetition will be possible (we should think, that for example, it is common to find
an obelisk in every country), so we leave that value as Duplicate.
Having done this, when we press F5 the database should be reorganized to create that new
index.
We should remember that the report’s navigation listing told us that we had no index to satisfy
the query. Now we will see what it tells us after the reorganization takes place.
Video recorded with GeneXus X Evolution 2
Page
We can delete it anytime, just as easily as we created it.
8
It tells us that it will use the newly-created index.
F5.
Reorganize.
And the navigation listing will inform us again that there is no index to satisfy that order…
Page
9
{Ppts}
Video recorded with GeneXus X Evolution 2
Database manager systems often store statistics of data and accesses which provide them with
some intelligence to select the best access plan, based on the attributes that are part of the
query. To see this aspect in detail, based on the generator used, we recommend you do a
search in our documents.
In this case, it will be enough to know that if we specify an order and there is no index created,
then a temporary index will be created to be used for the query and it will be discarded
afterwards, or the DBMS will solve this with some other strategy.
So what must we do to request a descending order?
Page
10
We just put the attribute inside parenthesis.
Video recorded with GeneXus X Evolution 2