@@ -117,6 +117,55 @@ Then try to put another row:
117
117
This ``INSERT `` fails because of a primary-key violation: the row with the primary
118
118
key ``1, 'AB' `` already exists.
119
119
120
+ The SEQSCAN keyword
121
+ ~~~~~~~~~~~~~~~~~~~
122
+
123
+ In Tarantool, ``SELECT `` SQL queries that perform sequential scans (that is, go
124
+ through all the table rows instead of using indexes) are prohibited by default.
125
+ For example, this query leads to the error ``Scanning is not allowed for 'table2' ``:
126
+
127
+ .. code-block :: sql
128
+
129
+ SELECT * FROM table2;
130
+
131
+ To execute a scan query, put the ``SEQSCAN `` keyword before the table name:
132
+
133
+ .. code-block :: sql
134
+
135
+ SELECT * FROM SEQSCAN table2;
136
+
137
+ Try to execute these queries that use indexed ``column1 `` in filters:
138
+
139
+ .. code-block :: sql
140
+
141
+ SELECT * FROM table2 WHERE column1 = 1;
142
+ SELECT * FROM table2 WHERE column1 + 1 = 2;
143
+
144
+ The result is:
145
+
146
+ * The first query returns rows:
147
+
148
+ .. code-block :: tarantoolsession
149
+
150
+ - [1, 'AB', 'AB', 10.5]
151
+ - [1, 'CD', ' ', 10005]
152
+
153
+ * The second query fails with the error ``Scanning is not allowed for 'TABLE2' ``.
154
+ Although ``column1 `` is indexed, the expression ``column1 + 1 `` is not calculated
155
+ from the index, which makes this ``SELECT `` a scan query.
156
+
157
+ .. note ::
158
+
159
+ You can allow SQL scan queries without ``SEQSCAN `` for the current session
160
+ by running the command:
161
+
162
+ .. code-block :: sql
163
+
164
+ SET SESSION "sql_seq_scan" = true;
165
+
166
+
167
+ Learn more about using ``SEQSCAN `` in the :ref: `SQL FROM clause description <sql_from >`.
168
+
120
169
SELECT with ORDER BY clause
121
170
~~~~~~~~~~~~~~~~~~~~~~~~~~~
122
171
@@ -127,8 +176,11 @@ Retrieve the 4 rows in the table, in descending order by ``column2``, then
127
176
128
177
.. code-block :: sql
129
178
130
- SELECT * FROM table2 ORDER BY column2 DESC, column4 ASC;
179
+ SELECT * FROM SEQSCAN table2 ORDER BY column2 DESC, column4 ASC;
180
+
181
+ .. important ::
131
182
183
+ Tarantool has its own
132
184
The result is:
133
185
134
186
.. code-block :: tarantoolsession
@@ -152,9 +204,9 @@ Retrieve some of what you inserted:
152
204
153
205
.. code-block :: sql
154
206
155
- SELECT column1, column2, column1 * column4 FROM table2 WHERE column2
207
+ SELECT column1, column2, column1 * column4 FROM SEQSCAN table2 WHERE column2
156
208
LIKE 'A%';
157
- SELECT column1, column2, column3, column4 FROM table2
209
+ SELECT column1, column2, column3, column4 FROM SEQSCAN table2
158
210
WHERE (column1 < 2 AND column4 < 10)
159
211
OR column3 = X'2020';
160
212
@@ -185,7 +237,7 @@ The rows which have the same values for ``column2`` are grouped and are aggregat
185
237
.. code-block :: sql
186
238
187
239
SELECT column2, SUM(column4), COUNT(column4), AVG(column4)
188
- FROM table2
240
+ FROM SEQSCAN table2
189
241
GROUP BY column2;
190
242
191
243
The result is:
@@ -251,8 +303,8 @@ from the result table.
251
303
252
304
CREATE TABLE table3 (column1 INTEGER, column2 VARCHAR(100), PRIMARY KEY
253
305
(column2));
254
- INSERT INTO table3 SELECT column1, column2 FROM table2 WHERE column1 <> 2;
255
- SELECT * FROM table3;
306
+ INSERT INTO table3 SELECT column1, column2 FROM SEQSCAN table2 WHERE column1 <> 2;
307
+ SELECT * FROM SEQSCAN table3;
256
308
257
309
The result is:
258
310
@@ -274,8 +326,8 @@ present in ``table3``.
274
326
275
327
.. code-block :: sql
276
328
277
- SELECT * FROM table2 WHERE (column1, column2) NOT IN (SELECT column1,
278
- column2 FROM table3);
329
+ SELECT * FROM SEQSCAN table2 WHERE (column1, column2) NOT IN (SELECT column1,
330
+ column2 FROM SEQSCAN table3);
279
331
280
332
The result is the single row that was excluded when inserting the rows with
281
333
the ``INSERT ... SELECT `` statement:
@@ -295,7 +347,7 @@ column values from another table.
295
347
296
348
.. code-block :: sql
297
349
298
- SELECT * FROM table2, table3
350
+ SELECT * FROM SEQSCAN table2, table3
299
351
WHERE table2.column1 = table3.column1 AND table2.column2 = table3.column2
300
352
ORDER BY table2.column4;
301
353
@@ -327,7 +379,7 @@ containing ``13`` in ``column2``. Then try to insert such a row.
327
379
INSERT INTO table4 VALUES (12, 13);
328
380
329
381
Result: the insert fails, as it should, with the message
330
- ``Check constraint failed '' ck_unnamed_TABLE4_1'': column2 <> 13 ``.
382
+ ``Check constraint ' ck_unnamed_TABLE4_1' failed for tuple ``.
331
383
332
384
CREATE TABLE with a FOREIGN KEY clause
333
385
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -348,7 +400,7 @@ Result:
348
400
* The first ``INSERT `` statement succeeds because
349
401
``table3 `` contains a row with ``[2, 'AB', ' ', 12.34567] ``.
350
402
* The second ``INSERT `` statement, correctly, fails with the message
351
- ``Failed to execute SQL statement: FOREIGN KEY constraint failed ``.
403
+ ``Foreign key constraint ''fk_unnamed_TABLE5_1'' failed: foreign tuple was not found ``.
352
404
353
405
UPDATE
354
406
~~~~~~
@@ -361,7 +413,7 @@ Use ``SELECT`` to see what happened to ``column4``.
361
413
.. code-block :: sql
362
414
363
415
UPDATE table2 SET column4 = column4 + 5 WHERE column4 <> 0;
364
- SELECT column4 FROM table2 ORDER BY column4;
416
+ SELECT column4 FROM SEQSCAN table2 ORDER BY column4;
365
417
366
418
The result is: ``{NULL, NULL, 0, 10.5, 17.34567, 10005} ``.
367
419
@@ -385,11 +437,11 @@ Try to delete the last and first of these rows:
385
437
386
438
DELETE FROM table2 WHERE column1 = 2;
387
439
DELETE FROM table2 WHERE column1 = -1000;
388
- SELECT COUNT(column1) FROM table2;
440
+ SELECT COUNT(column1) FROM SEQSCAN table2;
389
441
390
442
The result is:
391
443
392
- * The first ``DELETE `` statement causes an error message because
444
+ * The first ``DELETE `` statement causes an error because
393
445
there's a foreign-key constraint.
394
446
* The second ``DELETE `` statement succeeds.
395
447
* The ``SELECT `` statement shows that there are 5 rows remaining.
@@ -461,7 +513,7 @@ in many ways. For example:
461
513
462
514
.. code-block :: sql
463
515
464
- SELECT column2, column2 || column2, SUBSTR(column2, 2, 1) FROM table2;
516
+ SELECT column2, column2 || column2, SUBSTR(column2, 2, 1) FROM SEQSCAN table2;
465
517
466
518
The result is:
467
519
@@ -479,12 +531,12 @@ Number operations
479
531
You can also manipulate number data (usually defined with ``INTEGER ``
480
532
or ``DOUBLE `` data types) in many ways. For example:
481
533
482
- * shift lleft with the ``<< `` operator
534
+ * shift left with the ``<< `` operator
483
535
* get modulo with the ``% `` operator
484
536
485
537
.. code-block :: sql
486
538
487
- SELECT column1, column1 << 1, column1 << 2, column1 % 2 FROM table2;
539
+ SELECT column1, column1 << 1, column1 << 2, column1 % 2 FROM SEQSCAN table2;
488
540
489
541
The result is:
490
542
@@ -516,7 +568,7 @@ with arithmetic on a number column and ordering by a string column.
516
568
INSERT INTO t6 VALUES (+1234567890, 'GD', 1e30);
517
569
INSERT INTO t6 VALUES (10, 'FADEW?', 0.000001);
518
570
INSERT INTO t6 VALUES (5, 'ABCDEFG', NULL);
519
- SELECT column1 + 1, column2, column4 * 2 FROM t6 ORDER BY column2;
571
+ SELECT column1 + 1, column2, column4 * 2 FROM SEQSCAN t6 ORDER BY column2;
520
572
521
573
The result is:
522
574
@@ -537,8 +589,8 @@ Create a view ``v3`` based on ``table3`` and select from it:
537
589
538
590
.. code-block :: sql
539
591
540
- CREATE VIEW v3 AS SELECT SUBSTR(column2,1,2), column4 FROM t6 WHERE
541
- column4 >= 0;
592
+ CREATE VIEW v3 AS SELECT SUBSTR(column2,1,2), column4 FROM SEQSCAN t6
593
+ WHERE column4 >= 0;
542
594
SELECT * FROM v3;
543
595
544
596
The result is:
@@ -560,8 +612,8 @@ Create such a view and select from it:
560
612
.. code-block :: sql
561
613
562
614
WITH cte AS (
563
- SELECT SUBSTR(column2,1,2), column4 FROM t6 WHERE column4
564
- >= 0)
615
+ SELECT SUBSTR(column2,1,2), column4 FROM SEQSCAN t6
616
+ WHERE column4 >= 0)
565
617
SELECT * FROM cte;
566
618
567
619
The result is the same as the ``CREATE VIEW `` result:
@@ -593,17 +645,18 @@ The result of both these statements is:
593
645
Metadata
594
646
~~~~~~~~
595
647
596
-
597
648
To find out the internal structure of the Tarantool database with SQL,
598
- select from the Tarantool system tables:
649
+ select from the Tarantool system tables ``_space ``, ``_index ``, and ``_trigger ``:
650
+
651
+ .. code-block :: sql
599
652
600
- * tables: `` SELECT * FROM "_space"; ``
601
- * indexes: `` SELECT * FROM "_index"; ``
602
- * triggers: `` SELECT * FROM "_trigger"; ``
653
+ SELECT * FROM SEQSCAN "_space";
654
+ SELECT * FROM SEQSCAN "_index";
655
+ SELECT * FROM SEQSCAN "_trigger";
603
656
604
657
Actually, these statements select from NoSQL "system spaces".
605
658
606
- Select from ``_space ``:
659
+ Select from ``_space `` by a table name :
607
660
608
661
.. code-block :: sql
609
662
@@ -636,7 +689,7 @@ You can invoke SQL statements using the Lua function ``box.execute(string)``.
636
689
637
690
.. code-block :: tarantoolsession
638
691
639
- tarantool> box.execute([[SELECT * FROM table3;]]);
692
+ tarantool> box.execute([[SELECT * FROM SEQSCAN table3;]]);
640
693
641
694
The result is:
642
695
@@ -684,7 +737,7 @@ a bit:
684
737
start_time = os.clock();
685
738
main_function();
686
739
end_time = os.clock();
687
- 'insert done in ' .. end_time - start_time .. ' seconds';
740
+ print( 'insert done in ' .. end_time - start_time .. ' seconds') ;
688
741
689
742
The result is: you now have a table with a million rows, with a message saying
690
743
"``insert done in 88.570578 seconds ``".
@@ -701,7 +754,7 @@ Check how ``SELECT`` works on the million-row table:
701
754
.. code-block :: lua
702
755
703
756
box.execute([[SELECT * FROM tester WHERE s1 = 73446;]]);
704
- box.execute([[SELECT * FROM tester WHERE s2 LIKE 'QFML%';]]);
757
+ box.execute([[SELECT * FROM SEQSCAN tester WHERE s2 LIKE 'QFML%';]]);
705
758
706
759
The result is:
707
760
0 commit comments