Pages

Thursday, April 16, 2015

Repeating another SQLCL EA release

    It's been a month and a lot of bugs have been fixed in that time and new features added. That adds up to time for another EA for our new sqlcl.  The third sqlcl EA is out now and can be found here on the SQL Developer 4.1 download page.  

  Not sure what this sqlcl thing is ? Check here.


LOAD:  Making CSV loading to the db very easy

Here's a very quick example of the possibilities.  Here 

-- drop it to be sure
drop table emp2;

-- create an empty table
create table emp2 as select * from emp where rownum=0;

-- don't need to see it
set term off
-- set the format to CSV
set sqlformat csv
-- spool
spool emp.csv
-- spooling....
select * from emp;
-- spooled
spool off

-- need to see again
set term on
-- load into the new table
load EMP2 emp.csv



The output from running the script makes it very easy to see what happened.



REPEAT : Build your own monitor

This is another very simple yet useful command.  The idea is to basically build your own sql that your may want to run to see what's going on.  This example is a simple select against v$session with a decode on the command to see what the sessions are doing.  Running this is easy, running it again and again mean hitting / and return a lot.

Enter the repeat.  Just specify how many times and what the sleep interval is and sqlcl takes care of running it.



In the header, there's a status to who what iteration it is what the time is ( and yes I know a bug in the number format ) and what the delay is to running it again.




The Report being used

The SQL I'm using here is the same sql in the SQL Developer Sessions report.  It can be found off the Tools->Sessions menu.  Additionally in the Reports section under DBA reports.



select vs.sid ,serial# serial, vs.sql_id,
       vs.username "Username",
       case when vs.status = 'ACTIVE' then last_call_et else null end "Seconds in Wait",
       decode(vs.command,  
                         0,null, 
                         1,'CRE TAB', 
                         2,'INSERT', 
                         3,'SELECT', 
                         4,'CRE CLUSTER', 
                         5,'ALT CLUSTER', 
                         6,'UPDATE', 
                         7,'DELETE', 
                         8,'DRP CLUSTER', 
                         9,'CRE INDEX', 
                         10,'DROP INDEX', 
                         11,'ALT INDEX', 
                         12,'DROP TABLE', 
                         13,'CRE SEQ', 
                         14,'ALT SEQ', 
                         15,'ALT TABLE', 
                         16,'DROP SEQ', 
                         17,'GRANT', 
                         18,'REVOKE', 
                         19,'CRE SYN', 
                         20,'DROP SYN', 
                         21,'CRE VIEW', 
                         22,'DROP VIEW', 
                         23,'VAL INDEX', 
                         24,'CRE PROC', 
                         25,'ALT PROC', 
                         26,'LOCK TABLE', 
                         28,'RENAME', 
                         29,'COMMENT', 
                         30,'AUDIT', 
                         31,'NOAUDIT', 
                         32,'CRE DBLINK', 
                         33,'DROP DBLINK', 
                         34,'CRE DB', 
                         35,'ALTER DB', 
                         36,'CRE RBS', 
                         37,'ALT RBS', 
                         38,'DROP RBS', 
                         39,'CRE TBLSPC', 
                         40,'ALT TBLSPC', 
                         41,'DROP TBLSPC', 
                         42,'ALT SESSION', 
                         43,'ALT USER', 
                         44,'COMMIT', 
                         45,'ROLLBACK', 
                         46,'SAVEPOINT', 
                         47,'PL/SQL EXEC', 
                         48,'SET XACTN', 
                         49,'SWITCH LOG', 
                         50,'EXPLAIN', 
                         51,'CRE USER', 
                         52,'CRE ROLE', 
                         53,'DROP USER', 
                         54,'DROP ROLE', 
                         55,'SET ROLE', 
                         56,'CRE SCHEMA', 
                         57,'CRE CTLFILE', 
                         58,'ALTER TRACING', 
                         59,'CRE TRIGGER', 
                         60,'ALT TRIGGER', 
                         61,'DRP TRIGGER', 
                         62,'ANALYZE TAB', 
                         63,'ANALYZE IX', 
                         64,'ANALYZE CLUS', 
                         65,'CRE PROFILE', 
                         66,'DRP PROFILE', 
                         67,'ALT PROFILE', 
                         68,'DRP PROC', 
                         69,'DRP PROC', 
                         70,'ALT RESOURCE', 
                         71,'CRE SNPLOG', 
                         72,'ALT SNPLOG', 
                         73,'DROP SNPLOG', 
                         74,'CREATE SNAP', 
                         75,'ALT SNAP', 
                         76,'DROP SNAP', 
                         79,'ALTER ROLE', 
                         79,'ALTER ROLE', 
                         85,'TRUNC TAB', 
                         86,'TRUNC CLUST', 
                         88,'ALT VIEW', 
                         91,'CRE FUNC', 
                         92,'ALT FUNC', 
                         93,'DROP FUNC', 
                         94,'CRE PKG', 
                         95,'ALT PKG', 
                         96,'DROP PKG', 
                         97,'CRE PKG BODY', 
                         98,'ALT PKG BODY', 
                         99,'DRP PKG BODY', 
                         to_char(vs.command)) "Command",
    vs.machine "Machine",
    vs.osuser "OS User", 
    lower(vs.status) "Status",
    vs.program "Program",
    vs.module "Module",
    vs.action "Action",
    vs.resource_consumer_group,
    vs.client_info,
    vs.client_identifier
from gv$session vs 
where vs.USERNAME is not null
and nvl(vs.osuser,'x') <> 'SYSTEM'
and vs.type <> 'BACKGROUND'
order by 1;

Friday, January 30, 2015

3 new things about sdsql


New Name !


The first is a new name this EA it's named sqlcl for sql command line.  However, the binary to start it up is simply sql.  Nothing is easier when you need to run some sql than typing 'sql' and hitting enter.


#./sql klrice/klrice@//localhost/orcl

SQLcl: Release 4.1.0 Beta on Fri Jan 30 12:53:05 2015

Copyright (c) 1982, 2015, Oracle.  All rights reserved.


Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 


There's never too much Information


The next is a new information command has a lot more in it now.  Hit me up on twitter if you can think of more things it should show some of the things here are directly from folks doing exactly that. Sometimes functional specs can be done in 140 chars.



KLRICE@ORCL>
KLRICE@ORCL>info hr.employees
TABLE: EMPLOYEES 
  LAST ANALYZED:2015-01-17 12:06:26.0 
  ROWS         :107 
  SAMPLE SIZE  :107 
  INMEMORY     :DISABLED 
  COMMENTS     :employees table. Contains 107 rows. References with departments,
                       jobs, job_history tables. Contains a self reference. 

Columns 
NAME             DATA TYPE           NULL  DEFAULT    COMMENTS
*EMPLOYEE_ID     NUMBER(6,0)         No               Primary key of employees table.
 FIRST_NAME      VARCHAR2(20 BYTE)   Yes              First name of the employee. A not null column.
 LAST_NAME       VARCHAR2(25 BYTE)   No               Last name of the employee. A not null column.
 EMAIL           VARCHAR2(25 BYTE)   No               Email id of the employee
 PHONE_NUMBER    VARCHAR2(20 BYTE)   Yes              Phone number of the employee; includes country code and area code
 HIRE_DATE       DATE                No               Date when the employee started on this job. A not null column.
 JOB_ID          VARCHAR2(10 BYTE)   No               Current job of the employee; foreign key to job_id column of the
                                                      jobs table. A not null column.
 SALARY          NUMBER(8,2)         Yes              Monthly salary of the employee. Must be greater
                                                      than zero (enforced by constraint emp_salary_min)
 COMMISSION_PCT  NUMBER(2,2)         Yes              Commission percentage of the employee; Only employees in sales
                                                      department elgible for commission percentage
 MANAGER_ID      NUMBER(6,0)         Yes              Manager id of the employee; has same domain as manager_id in
                                                      departments table. Foreign key to employee_id column of employees table.
                                                      (useful for reflexive joins and CONNECT BY query)
 DEPARTMENT_ID   NUMBER(4,0)         Yes              Department id where employee works; foreign key to department_id
                                                      column of the departments table

Indexes
INDEX_NAME            UNIQUENESS  STATUS  FUNCIDX_STATUS  COLUMNS                COLUMN_EXPRESSION  
HR.EMP_JOB_IX         NONUNIQUE   VALID                   JOB_ID                                    
HR.EMP_NAME_IX        NONUNIQUE   VALID                   LAST_NAME, FIRST_NAME                     
HR.EMP_EMAIL_UK       UNIQUE      VALID                   EMAIL                                     
HR.EMP_EMP_ID_PK      UNIQUE      VALID                   EMPLOYEE_ID                               
HR.EMP_MANAGER_IX     NONUNIQUE   VALID                   MANAGER_ID                                
HR.EMP_DEPARTMENT_IX  NONUNIQUE   VALID                   DEPARTMENT_ID                             


References
TABLE_NAME   CONSTRAINT_NAME  DELETE_RULE  STATUS   DEFERRABLE      VALIDATED  GENERATED  
DEPARTMENTS  DEPT_MGR_FK      NO ACTION    ENABLED  NOT DEFERRABLE  VALIDATED  USER NAME  
EMPLOYEES    EMP_MANAGER_FK   NO ACTION    ENABLED  NOT DEFERRABLE  VALIDATED  USER NAME  
JOB_HISTORY  JHIST_EMP_FK     NO ACTION    ENABLED  NOT DEFERRABLE  VALIDATED  USER NAME  

KLRICE@ORCL>




Info doesn't end there, you can do info+ and get different data and I'm in the process of changing the indexes to show some stats also like the main table.

KLRICE@ORCL>info+ hr.employees
TABLE: EMPLOYEES 
  LAST ANALYZED:2015-01-17 12:06:26.0 
  ROWS         :107 
  SAMPLE SIZE  :107 
  INMEMORY     :DISABLED 
  COMMENTS     :employees table. Contains 107 rows. References with departments,
                       jobs, job_history tables. Contains a self reference. 

Columns 
NAME             DATA TYPE           NULL  DEFAULT    LOW_VALUE             HIGH_VALUE            NUM_DISTINCT   HISTOGRAM  
*EMPLOYEE_ID     NUMBER(6,0)         No               100                   206                   107            NONE       
 FIRST_NAME      VARCHAR2(20 BYTE)   Yes              Adam                  Winston               91             NONE       
 LAST_NAME       VARCHAR2(25 BYTE)   No               Abel                  Zlotkey               102            NONE       
 EMAIL           VARCHAR2(25 BYTE)   No               ABANDA                WTAYLOR               107            NONE       
 PHONE_NUMBER    VARCHAR2(20 BYTE)   Yes              011.44.1343.329268    650.509.4876          107            NONE       
 HIRE_DATE       DATE                No               1987.06.17.00.00.00   2000.04.21.00.00.00   98             NONE       
 JOB_ID          VARCHAR2(10 BYTE)   No               AC_ACCOUNT            ST_MAN                19             NONE       
 SALARY          NUMBER(8,2)         Yes              2100                  24000                 57             NONE       
 COMMISSION_PCT  NUMBER(2,2)         Yes              .1                    .4                    7              NONE       
 MANAGER_ID      NUMBER(6,0)         Yes              100                   205                   18             NONE       
 DEPARTMENT_ID   NUMBER(4,0)         Yes              10                    110                   11             NONE       

Indexes
INDEX_NAME            UNIQUENESS  STATUS  FUNCIDX_STATUS  COLUMNS                COLUMN_EXPRESSION  
HR.EMP_JOB_IX         NONUNIQUE   VALID                   JOB_ID                                    
HR.EMP_NAME_IX        NONUNIQUE   VALID                   LAST_NAME, FIRST_NAME                     
HR.EMP_EMAIL_UK       UNIQUE      VALID                   EMAIL                                     
HR.EMP_EMP_ID_PK      UNIQUE      VALID                   EMPLOYEE_ID                               
HR.EMP_MANAGER_IX     NONUNIQUE   VALID                   MANAGER_ID                                
HR.EMP_DEPARTMENT_IX  NONUNIQUE   VALID                   DEPARTMENT_ID                             


References
TABLE_NAME   CONSTRAINT_NAME  DELETE_RULE  STATUS   DEFERRABLE      VALIDATED  GENERATED  
DEPARTMENTS  DEPT_MGR_FK      NO ACTION    ENABLED  NOT DEFERRABLE  VALIDATED  USER NAME  
EMPLOYEES    EMP_MANAGER_FK   NO ACTION    ENABLED  NOT DEFERRABLE  VALIDATED  USER NAME  
JOB_HISTORY  JHIST_EMP_FK     NO ACTION    ENABLED  NOT DEFERRABLE  VALIDATED  USER NAME  

KLRICE@ORCL>


Those who don't know history are doomed to repeat.


The last thing to be sure to take a look at is the history. History by itself is great however we wanted to show how often the history is uses so there's now "history usage" which will show how many times that history item has been run.  The other thing new here is that instead of timing statements only when user request it, we now time and record that time for everything.  You can access is very simply with "history time" that will show the timings of how long the things in the history took.  The format of the time is HH:MI:SS.fff and we shorten the leading numbers when zero to be able to read and format it easier.






KLRICE@ORCL>history time
....
 20           info s
 21  (00.050) select table_name from user_tables
 22           info+ s
 23           info hr.dept
 24           into scott.emp
 25           info scott.emp
 26           info+ scott.emp
 27           info all_tables
 28           info all_tab_comments
 29  (04.531) select * from all_tables t,all_tab_comments tc where t.owner = tc.owner and t.table_name = tc.table_name
 30  (00.420) select * from all_tables t,all_tab_comments tc  where t.owner = tc.owner (+) and t.table_name = tc.table_name(+) 



While this is 3 really quick things, there's a lot more in there with this new EA. We will blog the other new things more in the coming days.

Wednesday, December 17, 2014

SDSQL - Output Formats

   In SQL Developer, Jeff has shown in many tips & tricks talks and here on his blog how to get data from any select into a few formats.  The most common I would say is csv.   Nothing here has changed. Well, almost.  What will be new is the html format is getting a facelift and a json format is being added neither are in the version of sdsql or sql developer that are on otn yet

    As expected all those "hint" work in sdsql as well.  What's new is there is a "set sqlformat" command so that all results come out in that format. So, all the same formats work : xml, html, delimited, insert, loader, fixed, text. Also there's a new one named ansiconsole which is described here.





  Now with the new things being added it could be easy to create a "build" script of sorts.  Combine this with the new ddl command into something like this

SQL>set sqlformat insert
SQL>spool my_build_script.sql
SQL>ddl emp
SQL>select * from emp;
SQL>apex export 101
SQL>spool off

What you would end up with out of this is a sql script with the ddl and data for emp and the Apex App export.  This script could then be checked in, given to a DBA, or even just for a nightly backup of sorts.

Tuesday, December 16, 2014

SDSQL - Pre and Post Commands

  This is in the process of evolving but should be enough for anyone to give it a whirl.  The idea is that for any given statement you may want to run something before and possible after.

  Here's a trivial example.  I'm issuing 2 statements 1) a drop table and 2) creating a table of select * from v$mystat before each command.  Then after I'm doing sql that shows me the changes from that point to now.

SQL> select count(1) from emp;

Table MY_STATS dropped.


Table MY_STATS created.

  COUNT(1)
----------
        14

NAME                                                                  DELTA
---------------------------------------------------------------- ----------
logical read bytes from cache                                       1859584
session pga memory                                                  1507328
session uga memory                                                  1035504
KTFB alloc space (block)                                              65536
cell physical IO interconnect bytes                                   32768
physical write total bytes                                            24576
physical write bytes                                                  24576
redo size                                                             16200
.....


This is on the same connection you are executing on so whatever is being done will show in the stats as well. One thing still being worked on is the ability to clone and pass some context to another connect and have these pre and post things done on that connection keeping the main connection free of that noise.

The way to do this is the introduction of 2 new set commands. The names should be a clue as to which is which : set precommand and set postcommand. They can hold 1 or N commands to be run.

set precommand drop table my_stats; create table my_stats as select * from v$mystat;
set postcommand select s.name, ( m2.value - m.value ) delta from my_stats m, v$mystat m2, V$STATNAME s where m.STATISTIC#   = m2.STATISTIC# and m.STATISTIC#    = s.STATISTIC# and m.value != m2.value order  by 2 desc;

select count(1) from user_objects where object_name like 'E%';
~                                                                 

The current downside of this method is that it needs to be all on one line.  So, if there's multiple commands to be run it's kinda ugly.  Like in this precommand where I drop and create a table.  The great thing about this is that the command to be run could be @mypre.sql

This allows me to better format the sql and make it much much easier to read and maintain.

└─>cat pre.sql 
drop table my_stats;
create table my_stats as select * from v$mystat;

└─>cat post.sql 
select s.name, ( m2.value - m.value ) delta 
  from my_stats m, v$mystat m2, V$STATNAME s 
  where m.STATISTIC#   = m2.STATISTIC# 
    and m.STATISTIC#    = s.STATISTIC# 
    and m.value != m2.value order  by 2 desc;


Then the sql set command look just like expected.

set precommand @pre.sql
set postcommand @post.sql



Now if you want to mess with people throw something like this in their login.sql when they aren't looking

SQL> set postcommand select 'Better go tune that before Joel sees it' MOTD  from dual;


Then on every command they will get that message.

SQL> select 1 from dual;


         1
----------
         1

MOTD                                 
---------------------------------------
Better go tune that before Joel sees it

SQL> 

Friday, December 12, 2014

Aliases with sdsql and simpler CTAS

First, we just put up a new build of sdsql.  Go get it or the last thing here will not work.

  SQL is a great and verbose language so there's many ways to shorten what we have to type.  As simple as a view or saving a script to call later with @/path/to/sessions.sql  SDSQL is taking it a step further and we added aliases to the tool.  Almost as if right on queue, John asked if we could add them.



Here's a simple example where I setup an alias for "sessions" in my login.sql.  Once defined  you simply type that alias name hit enter and go.  Now do not go and be trying to alias "select" or other base things.  Aliases are evaluated after everything we normally recognize , although it would be an awesome April fool thing to edit a friend's file and do that. hmm maybe a new feature....





Setting up these aliases are very simple yet quite flexible.  In the simplest form,


KLRICE@ORCL>alias ls=select object_name,object_type,last_ddl_time 
  2  from user_objects
  3   order by 2,3;
KLRICE@ORCL>ls
Command=ls
OBJECT_NAME                     OBJECT_TYPE        LAST_DDL_TIME  
REMOTEDB                        DATABASE LINK                     
DEMO_ERROR_HANDLING             FUNCTION           25-SEP-14      
DO_WORK                         FUNCTION           25-SEP-14      
EXECSQL                         FUNCTION           25-SEP-14      
FIB                             FUNCTION           25-SEP-14      

Now add in binds and it become very flexible.  This is just one bind but you can probably guess the names of binds 2,3,N

KLRICE@ORCL>alias ll=select object_name,object_type,last_ddl_time 
  2  from user_objects
  3  where object_name like :1||'%';
KLRICE@ORCL>ll E
Command=ll
OBJECT_NAME    OBJECT_TYPE  LAST_DDL_TIME  
EXECSQL        FUNCTION     25-SEP-14      
EMP_TRG1       TRIGGER      25-SEP-14      
EMP_SEQ        SEQUENCE     25-SEP-14      
EMP_NEW        TABLE        11-DEC-14      
EMPLOYEES_SEQ  SEQUENCE     25-SEP-14      
EMPLOYEES_PK   INDEX        25-SEP-14      
EMPLOYEES22    TABLE        25-SEP-14      
EMP            TABLE        25-SEP-14      
E1             TABLE        11-DEC-14      


It works for aliasing off plsql calls also.


If you need to see the aliases defined, just type alias or alias list.




If you need to see what the alias is, just type alias list <alias name>



There's lot you can do with this to save lots of typing and personally there'll be lots less typo for my presentations!


CTAS Easier than ever


Lastly, for the one that went and downloaded the build we just posted today there is a new CTAS <TABLE> <NEW_TABLE> command added just yesterday.  The idea here is yet again to save on typing.  This is just getting dbms_metadata information so what is used can be adjusted as I explained here  Then strip off the things not needed ( i.e. datatypes ) swap out the names and you get a new table with very very little typing.  Note the index on empno carries over.