|
|
Want to know more about Packt's Article Network? Interested in contributing your article ideas? Please visit our FAQ for more information. See More BROWSE
All Titles WordPress Web Services SOA BPEL Web Graphics & Video Web Development RAW Portugues, Espanol, Italiano, French PHP/MySQL Oracle Open Source Networking & Telephony Moodle Microsoft & .NET Linux Servers jQuery Joomla! JBoss Java e-Learning e-Commerce Dynamics Drupal CRM Cookbook Content Management Beginner Guides Architecture and Analysis AJAX Future Titles Recently Published Titles MySQL Query Browser, one of the open source MySQL GUI tools from MySQL AB, is used for building MySQL database queries visually. In MySQL Query Browser, you build database queries using just your mouse—click, drag and drop! When you’re creating a query for non-technical users and you don’t want them to see the complexity of the query, you can create a view from the query. Your users can then use the view as a table; for example, they can double-click the view, which will effectively execute its complex query behind the scene. In MySQL Query Browser, you have two ways to create a view, which Djoni Darmawikarta will demonstrate in this article:
See More |
Comparing Cursor and Set Approaches in Processing Relational Data
To give you an idea about cursor, the following DECLARE statement creates a cursor (named zero_bal_crs), which gives you access to the rows of a payment table, rows that have 100 or less balances. DECLARE zero_bal_crs CURSOR FOR You then fetch and process each of the rows sequentially; the process can be, for example, summing up rows by product group and loading the sums into a summary table. You develop the process outside of the cursor, for example, in a stored procedure. (You’ll see in the examples that the cursor-based process is procedural). Instead of processing row-by-row sequentially, you can process relational data set-by-set, without a cursor. For example, to sum all payment rows with 100 or less balances and load it into a table (named payment_100orless_bal in the following SQL statement), you can use the following SQL statement. This single SQL statement completely processes the rows that meet the condition, all at once, as a set. INSERT INTO payment_100orless_bal The following three examples, which are among those I most frequently encountered, further compare cursor-based processing with set processing. Example 1: Sequential Loop cf. One SQL StatementOur process in the first example is to summarize sales transactions by product. Listing 1 shows the DDLs for creating the sales transaction table and sales product table that stores the summary. Listing 1.1: DDLs of the Example 1 Tables CREATE TABLE sales_transactions Listing 1.2 shows a cursor-based stored procedure that implements the process. In this example, what the cursor (sales_crs) does is simply putting all rows in ascending order by their product codes. As you might have expected, this stored procedure applies loop with if-then-else programming construct to process the data row-by-row. Listing 1.2 Cursor Procedural solution DELIMITER $$ The stored procedure in Listing 1.3 implements a set, processing to accomplish the same purpose as its foregoing cursor-based processing. You can see that this stored procedure is simpler than its cursor-based counterpart. Listing 1.3 Set Operation solution DELIMITER // Example 2: Nested Cursor cf. JoinOur 2nd example is to consolidate order by customer. While example 1 has one table, example 2 has two: order and item. The DDLs of the two tables are shown in Listing 2. To process two tables, our cursor implementation uses a nested loop (Listing 2.2) which makes it even more complex than example 1. Listing 2.1: DDL’s of the Example 2 Tables CREATE TABLE order ( Listing 2.2: Nested Cursor DELIMITER $$ Listing 2.3 is the set solution. We apply join in this 2nd example which makes it just a single SQL statement. You can see that this set stored procedure is again simpler than its cursor-based equivalent. Creating your MySQL Database: Practical Design Tips and Techniques
Listing 2.3 Join Set Processing DELIMITER $$ Example 3: Join Cursor cf. Multi SetsIn the 3rd example we’d like to keep track of order payments. We update the payment once a month (30 days), and remove an order when its balance is fully paid (zero balance). We apply late charges (5%) to outstanding balances older than 90 days; and, to those that have outstanding balance older than 1 year, we suspend the customer (move them to a suspense table). Listing 3.1: DDL’s of the Example 3 Tables CREATE TABLE order_payment ( The cursor in the stored procedure (Listing 3.2) uses join, but we process the rows sequentially, not as a set at once. Listing 3.2: Join Cursor DELIMITER $$ While the cursor defines only one set, our set processing requires four sets (a set defines the rows that qualify for the conditions of an SQL operation):
Note that we need to apply two to SQL operations to the fourth set: add the orders to the suspense table and remove them from the payment balance table. Listing 3.3 Multi Operations Set Processing DELIMITER $$ Speed TestI tested both cursor-based and set stored procedures of all three examples with data ranges from a thousand to 100 thousand rows, and found out (as expected) that the cursor processing is 50 to 100 times slower than the set processing. You can likely be able to further speed up the set processing by applying relational database performance facilities, such as index. SummaryBoth cursor and set processing define set(s) of rows of the data to process; but, while cursor processes the rows sequentially, set processing takes its sets at once. Set operation is generally simpler and faster than cursor-based processing. The challenge, particularly to those developers accustomed to procedural process, is the need to design their solutions in terms of set and set operation instead of the more familiar sequential procedural algorithm. Creating your MySQL Database: Practical Design Tips and Techniques
About the AuthorDjoni Darmawikarta built his career in IBM Asia Pacific and Canada as a software engineer, international consultant, instructor and project manager, for a total of 17 years. He's currently a technical specialist in the Data Warehousing and Business Intelligence team of a Toronto-based insurance company. Outside of his office works, Djoni writes IT articles and books. Books from Packt | TOP TITLES ![]()
MySQL Query Browser, one of the open source MySQL GUI tools from MySQL AB, is used for building MySQL database queries visually. In MySQL Query Browser, you build database queries using just your mouse—click, drag and drop! MySQL Query Browser has plenty of visual query building functions and features. This article Djoni Darmawikarta shows two examples, building Join and Master-detail queries. These examples will demonstrate some of these functions and features. See More
|
| ||||||||