sql anywhere tips and tricks

31
(c) 2015 Independent SAP Technical User Group Annual Conference, 2015 SQL Anywhere Tips and Tricks Jason Hinsperger Product Manager, SAP

Upload: sap-technology

Post on 16-Aug-2015

99 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

SQL Anywhere Tips and Tricks

Jason HinspergerProduct Manager, SAP

Page 2: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

SQL Anywhere Tips and TricksAutocommit

C External Environments

Dedicated Tasks

sp_list_directory and friends

Variables in USING and AT clauses

Page 3: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

AUTOCOMMIT

Page 4: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

AutocommitAutocommit Off Chained

OnAutocommit On Chained

Off IT DEPENDS!For TDS connections Autocommit on/off is effectively equivalent to

Chained off/on jConnect, Open Client

For other connections, Autocommit and Chained are completely different

ODBC, SQLA JDBC, .NET, …Chained mode controls whether the server does a commit after each

statementAutocommit mode controls whether the client sends an explicit commit

after each execution

Page 5: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Autocommit VS Chained (ODBC Example)

CREATE PROCEDURE p( IN id INT, OUT numrows INT )

BEGIN

SAVEPOINT sv;

INSERT INTO tab2 SELECT * FROM tab1 WHERE tab1.tabid < id;

SELECT count(*) INTO numrows FROM tab2;

ROLLBACK TO SAVEPOINT sv;

END

If Autocommit is ON (driver default) and Chained is OFF (database default), then executing the above procedure via ODBC does not change the contents of tab2

If Autocommit is either ON or OFF and Chained is ON, then executing the above procedure via ODBC does change the contents of tab2 AND will generate an error

Page 6: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Turning Autocommit OffSo when we say “turn autocommit off” we actually mean make the API

call on the client ODBC: SQLSetConnectAttr( … SQL_ATTR_AUTOCOMMIT … ) JDBC: Connection.setAutoCommit( false ) (regardless of whether you

are using jConnect or SQLA JDBC) …

AND avoid messing with chained mode

Page 7: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

C EXTERNAL ENVIRONMENTS

Page 8: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

C External Environments vs In Process External Stored Procedures

SQL Anywhere has had the ability to load user provided dlls and shared objects for a long time

More recently these dlls and shared objects can now be loaded and executed outside of the server using C External Environments

Loading these dlls and shared objects outside of the server provides: Improved server stability Greater security of data Mixing and matching of server and dll/shared object bitness The ability to return result sets from the external stored procedure The ability to make server side calls via ODBC or ESQL Easier debugging of external dll/shared object

Page 9: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

C External Environments vs In Process External Stored Procedures

Changing an external stored procedure from in process to out of process is as simple as adding a LANGUAGE clause

CREATE PROCEDURE myextproc(…) EXTERNAL NAME ‘[email protected]

CREATE PROCEDURE myextproc(…) EXTERNAL NAME ‘[email protected]’ LANGUAGE C_ODBC64

EXTERNAL NAME clause can contain both Unix and Windows definitions – exactly the same as the in process EXTERNAL NAME clause

Page 10: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

C External Environments - DEMO

Page 11: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

C External Environments vs In-Process External Stored Procedures

Things to take into consideration when deciding to load dlls and shared object in C External Environment:

Time required to make a call out of process is significantly greater than making an in process call

Machine resource utilization is higher due to separate process per connection

Sharing of data across connections is not allowed

Page 12: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

DEDICATED TASKS

Page 13: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

ScenarioI have a large number of users who hammer away at my server

throughout the dayOf these users, I have a couple of admin users who get in earlier than

the rest and who must be given priority when they make server requests

The response time for my admin users decreases significantly when the rest of the users get in and start making constant server requests

How can I fix it so that my admin users are always given priority over the other users

I am not as concerned about the response time for requests from my non-admin users

Page 14: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Dedicated tasksOne way to solve this problem is to assigned dedicated server tasks to

the admin usersThis is done using the dedicated_task database optionThe admin users could be assigned a login procedure that automatically

sets the dedicated_task option to on for that connectionOr the admin users could be given permission to set and unset the

option on the fly as needed

Page 15: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Dedicated Tasks - DEMO

Page 16: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Dedicated tasksNote that dedicating a server task to a connection reduces the number

of server tasks available for servicing requests from other connectionsThis task cannot be used by the server for another connection EVEN IF

the connection that the task is dedicated to is not currently executing a request in the server

This feature should therefore be used with caution to avoid starvation of requests

However for those unique situations where a request absolutely must make it into the server; the dedicated_task option can be very useful

Page 17: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

SP_LIST_DIRECTORY AND FRIENDS

Page 18: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Preamble: Directory Access Servers

sp_list_directory() and friends can be used as a light weight replacement for directory access servers

Directory access servers allow applications to create proxy tables for “querying” and manipulating directories on the server machine

CREATE SERVER … CLASS ‘directory’ USING ‘root=…;subdirs=…’Some people find that directory access servers are cumbersome and too

powerful for what they really want to do

Page 19: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

sp_list_directory and friendsTo provide an alternate approach to querying and managing directories

on server machines, we added a set of file and directory stored procedures

Combined with xp_read_file() and xp_write_file(), this full set of stored procedures can provide the same functionality as directory access servers without requiring the creation of remote servers and extern logins

Directory procedures File procedures

dbo.sp_list_directory()

dbo.sp_copy_directory() dbo.sp_copy_file()

dbo.sp_move_directory() dbo.sp_move_file()

dbo.sp_delete_directory() dbo.sp_delete_file()

Page 20: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

sp_list_directory and friends - DEMO

Page 21: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

sp_list_directory and friends – final thoughts

In some ways, sp_list_directory() and friends are more efficient than directory access servers

They are also easier to use and do not require any setupTheir use can also be easily restricted via system privileges and secure

featuresHowever, directory access servers have certain optimizations that do

not currently exist with sp_list_directory() and friends Select … from dirtab where file_name=‘…’ Select … from dirtab where left(file_name, …) = ‘…’

But if all you want to do is list the contents of a directory, fetch some files or do some directory or file administration, sp_list_directory() and friends will usually provide the better alternative

Page 22: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

VARIABLES IN ‘USING’ AND ‘AT’ CLAUSES

Page 23: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Preamble: Driver = SQL Anywhere NativeWhen creating DSN-less remote servers to another SQL Anywhere

server, use Driver=SQL Anywhere Native instead of Driver=SQL Anywhere 16

eg. CREATE SERVER rem CLASS ‘saodbc’ USING ‘Driver=SQL Anywhere Native;host=…’

“Driver=SQL Anywhere Native” works on all platforms that support RDA Database can be moved from machine to machine and across platforms without requiring changes to the

remote server definition

ODBC Driver must be available on the machine but does not need to be registered

Server bypasses ODBC Driver Manager and loads the ODBC Driver directly

Bypassing ODBC Driver Manager gives a moderate (10%) perf. improvement

If multiple copies of the ODBC Driver are installed, then the server will pick the first one it finds (usually that is the one that was installed with that version of the server)

Page 24: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Preamble: ExternLogins1. When making a remote server connection, SQLA will first attempt to

use EXTERNLOGIN credentials of the current executing user2. If there are no EXTERNLOGIN credentials of the current executing

user, then SQLA will fail if the current executing user is not the the current logged in user

3. If the current executing user is the same as the current logged in user and there are no EXTERNLOGIN credentials for the current logged in user, then SQLA will attempt to use the local credentials of the current logged in user

These credentials are appended to the connection string that is sent to the underlying driver; hence any userid or password values in either the DSN or the USING clause will get overridden

CREATE SERVER … USING ‘Driver=SQL Anywhere Native;…;uid=…;pwd=…’ will not work because driver sees two sets of uid= and pwd= values and the last one wins

Page 25: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Preamble: ExternLoginsTrick: To fool SQL Anywhere into NOT appending uid= and pwd= and

instead using the uid= and pwd= in either the USING clause or the DSN; create a dummy EXTERN LOGIN with no remote login

CREATE EXTERNLOGIN myuser TO myserver Creates an externlogin for myuser to server myserver with no remote login/password

SQLA will see this externlogin and assume myuser has no remote userid/password for server myserver

As a result, SQLA will not append any uid= or pwd= when making a connection to myserver IF the current effective user is myuser

This allows the driver to use the uid/pwd that is in the USING clause or the DSN

Page 26: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Preamble: USING and AT Clauses

CREATE SERVER … CLASS ‘…’ USING ‘…’CREATE EXISTING TABLE … AT ‘…’CREATE PROCEDURE … AT ‘…’CREATE FUNCTION … AT ‘..’AT clauses are sometimes also called LOCATION clauses

Trick: CREATE TABLE table-name ( column-list ) AT ‘…’ will create both the proxy table and the remote table at the same time

Page 27: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Variables in USING and AT Clauses The USING and AT clauses can contain substitution variables which

get evaluated at run time These variables take the form {variable-name} and can appear

anywhere Variables in the USING and AT clause can be used to redefine or

change the remote server definition or proxy table/procedure location on the fly

This can be extremely useful if you have multiple remote servers, remote tables or remote procedures that you need to manage but do not want to have to set up individual remote server definitions, proxy table and/or proxy procedure definitions

Variables in USING and AT clauses can be used with any remote server and is not restricted to SQL Anywhere back ends

Page 28: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Variables in USING and AT Clauses - DEMO

Page 29: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Final ThoughtsThe features presented here are only some of the lesser known but

extremely useful features in SQL AnywhereA few other such features are:

Scripting external environments (PERL and PHP) TRY … CATCH dbo.sp_forward_to_remote_server() Sandboxing MERGE Secure features …

All of the features presented here and many more are available today

Page 30: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Final ThoughtsLet us know about your favorite SQL Anywhere feature and how you use

it http://scn.sap.com/community/sql-anywhere

Get involved in the SQL Anywhere forum: http://sqlanywhere-forum.sap.com

Page 31: SQL Anywhere Tips and Tricks

(c) 2015 Independent SAP Technical User Group

Annual Conference, 2015

Thank you

Jason [email protected]