Читаем MySQL: руководство профессионала полностью

Оптимизатор исследует, могут ли доступные пространственные индексы включаться в поиск для запросов, которые используют функцию типа MBRContains() или MBRWithin() в предложении WHERE. Следующий запрос находит все объекты, которые находятся в данном прямоугольнике:mysql> SET @poly = 'Polygon((30000 15000, 31000 15000, 31000 16000,

– > 30000 16000, 30000 15000))';

mysql> SELECT fid, AsText(g) FROM geom WHERE

– > MBRContains(GeomFromText(@poly), g);

+-----+---------------------------------------------------------------+

| fid | AsText(g) |

+-----+---------------------------------------------------------------+

| 21 | LINESTRING(30350.4 15828.8,30350.6 15845,30333.8 15845,30 … |

| 22 | LINESTRING(30350.6 15871.4,30350.6 15887.8,30334 15887.8, … |

| 23 | LINESTRING(30350.6 15914.2,30350.6 15930.4,30334 15930.4, … |

| 24 | LINESTRING(30290.2 15823,30290.2 15839.4,30273.4 15839.4, … |

| 25 | LINESTRING(30291.4 15866.2,30291.6 15882.4,30274.8 15882. … |

| 26 | LINESTRING(30291.6 15918.2,30291.6 15934.4,30275 15934.4, … |

| 249 | LINESTRING(30337.8 15938.6,30337.8 15946.8,30320.4 15946. … |

| 1 | LINESTRING(30250.4 15129.2,30248.8 15138.4,30238.2 15136. … |

| 2 | LINESTRING(30220.2 15122.8,3024.2 15137.8,30207.6 15136, … |

| 3 | LINESTRING(30179 15114.4,30176.6 15129.4,30167 15128,3016 … |

| 4 | LINESTRING(30155.2 15121.4,30140.4 15118.6,30142 15109,30 … |

| 5 | LINESTRING(30192.4 15085,30177.6 15082.2,30179.2 15072.4, … |

| 6 | LINESTRING(30244 15087,30229 15086.2,30229.4 15076.4,3024 … |

| 7 | LINESTRING(30200.6 15059.4,30185.6 15058.6,30186 15048.8, … |

| 10 | LINESTRING(30179.6 1504.8,30181 15002.8,30190.8 15003.6, … |

| 11 | LINESTRING(30154.2 15000.4,30168.6 15004.8,30166 15014.2, … |

| 13 | LINESTRING(30105 15065.8,30108.4 15050.8,30118 15053,3011 … |

| 154 | LINESTRING(30276.2 15143.8,30261.4 15141,30263 15131.4,30 … |

| 155 | LINESTRING(30269.8 15084,30269.4 15093.4,30258.6 15093,30 … |

| 157 | LINESTRING(30128.2 15011,30113.2 15010.2,30113.6 15000.4, … |

+-----+---------------------------------------------------------------+

20 rows in set (0.00 sec)


Использование EXPLAIN показывает, каким способом этот запрос выполнен:mysql> SET @poly = 'Polygon((30000 15000, 31000 15000,

– > 31000 16000, 30000 16000, 30000 15000))';

mysql> EXPLAIN SELECT fid, AsText(g) FROM geom WHERE

– > MBRContains(GeomFromText(@poly), g)\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: geom

type: range

possible_keys: g

key: g

key_len: 32

ref: NULL

rows: 50

Extra: Using where

1 row in set (0.00 sec)


Проверьте, что случилось бы без пространственного индекса:mysql> SET @poly = 'Polygon((30000 15000, 31000 15000,

– > 31000 16000, 30000 16000, 30000 15000))';

mysql> EXPLAIN SELECT fid,AsText(g) FROM g IGNORE INDEX (g) WHERE

– > MBRContains(GeomFromText(@poly), g)\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: geom

type: ALL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: 32376

Extra: Using where

1 row in set (0.00 sec)


Выполнение инструкции SELECT без пространственного индекса выдает тот же самый результат, но заставляет время выполнения повышаться с 0.00 до 0.46 секунды:mysql> SET @poly = 'Polygon((30000 15000, 31000 15000,

– > 31000 16000, 30000 16000, 30000 15000))';

mysql> SELECT fid, AsText(g) FROM geom IGNORE INDEX (g) WHERE

– > MBRContains(GeomFromText(@poly), g);

+-----+---------------------------------------------------------------+

| fid | AsText(g) |

+-----+---------------------------------------------------------------+

| 1 | LINESTRING(30250.4 15129.2,30248.8 15138.4,30238.2 15136. … |

| 2 | LINESTRING(30220.2 15122.8,3024.2 15137.8,30207.6 15136, … |

| 3 | LINESTRING(30179 15114.4,30176.6 15129.4,30167 15128,3016 … |

| 4 | LINESTRING(30155.2 15121.4,30140.4 15118.6,30142 15109,30 … |

| 5 | LINESTRING(30192.4 15085,30177.6 15082.2,30179.2 15072.4, … |

| 6 | LINESTRING(30244 15087,30229 15086.2,30229.4 15076.4,3024 … |

| 7 | LINESTRING(30200.6 15059.4,30185.6 15058.6,30186 15048.8, … |

| 10 | LINESTRING(30179.6 1504.8,30181 15002.8,30190.8 15003.6, … |

| 11 | LINESTRING(30154.2 15000.4,30168.6 15004.8,30166 15014.2, … |

| 13 | LINESTRING(30105 15065.8,30108.4 15050.8,30118 15053,3011 … |

| 21 | LINESTRING(30350.4 15828.8,30350.6 15845,30333.8 15845,30 … |

| 22 | LINESTRING(30350.6 15871.4,30350.6 15887.8,30334 15887.8, … |

| 23 | LINESTRING(30350.6 15914.2,30350.6 15930.4,30334 15930.4, … |

| 24 | LINESTRING(30290.2 15823,30290.2 15839.4,30273.4 15839.4, … |

| 25 | LINESTRING(30291.4 15866.2,30291.6 15882.4,30274.8 15882. … |

| 26 | LINESTRING(30291.6 15918.2,30291.6 15934.4,30275 15934.4, … |

| 154 | LINESTRING(30276.2 15143.8,30261.4 15141,30263 15131.4,30 … |

| 155 | LINESTRING(30269.8 15084,30269.4 15093.4,30258.6 15093,30 … |

| 157 | LINESTRING(30128.2 15011,30113.2 15010.2,30113.6 15000.4, … |

| 249 | LINESTRING(30337.8 15938.6,30337.8 15946.8,30320.4 15946. … |

+-----+---------------------------------------------------------------+

20 rows in set (0.46 sec)


Перейти на страницу:

Похожие книги

C# 4.0: полное руководство
C# 4.0: полное руководство

В этом полном руководстве по C# 4.0 - языку программирования, разработанному специально для среды .NET, - детально рассмотрены все основные средства языка: типы данных, операторы, управляющие операторы, классы, интерфейсы, методы, делегаты, индексаторы, события, указатели, обобщения, коллекции, основные библиотеки классов, средства многопоточного программирования и директивы препроцессора. Подробно описаны новые возможности C#, в том числе PLINQ, библиотека TPL, динамический тип данных, а также именованные и необязательные аргументы. Это справочное пособие снабжено массой полезных советов авторитетного автора и сотнями примеров программ с комментариями, благодаря которым они становятся понятными любому читателю независимо от уровня его подготовки. Книга рассчитана на широкий круг читателей, интересующихся программированием на C#.Введите сюда краткую аннотацию

Герберт Шилдт

Программирование, программы, базы данных
C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных
Programming with POSIX® Threads
Programming with POSIX® Threads

With this practical book, you will attain a solid understanding of threads and will discover how to put this powerful mode of programming to work in real-world applications. The primary advantage of threaded programming is that it enables your applications to accomplish more than one task at the same time by using the number-crunching power of multiprocessor parallelism and by automatically exploiting I/O concurrency in your code, even on a single processor machine. The result: applications that are faster, more responsive to users, and often easier to maintain. Threaded programming is particularly well suited to network programming where it helps alleviate the bottleneck of slow network I/O. This book offers an in-depth description of the IEEE operating system interface standard, POSIX (Portable Operating System Interface) threads, commonly called Pthreads. Written for experienced C programmers, but assuming no previous knowledge of threads, the book explains basic concepts such as asynchronous programming, the lifecycle of a thread, and synchronization. You then move to more advanced topics such as attributes objects, thread-specific data, and realtime scheduling. An entire chapter is devoted to "real code," with a look at barriers, read/write locks, the work queue manager, and how to utilize existing libraries. In addition, the book tackles one of the thorniest problems faced by thread programmers-debugging-with valuable suggestions on how to avoid code errors and performance problems from the outset. Numerous annotated examples are used to illustrate real-world concepts. A Pthreads mini-reference and a look at future standardization are also included.

David Butenhof

Программирование, программы, базы данных