Java Interview ⑦ Advanced section

Source: Internet
Author: User
Tags allkeys bulk insert dname volatile redis cluster redis server

Has 2.6.1 ever used Linux? What do you use it for?

Linux is a relatively stable operating system for a long time, so we usually take him as a server (Web,db,app, etc.).

Linux itself has a C compiler environment, some of our software is not package (Redis, etc.), need to compile in the Linux C compiler environment to get packages

2.6.2 say some of the common commands under Linux

Common:

PWD Gets the current path

CD Jump to Directory

Su-u Switch to Administrator

LS Enumeration Directory

File Operations Command:

File

Tail view

RM-RF Delete

Vim modification

Folder

mkdir Create

Rm-r Delete

2.6.3 What you use to connect to a remote Linux server

The connection remote needs to rely on the Linux server installs the SSH service side, generally this SSH service port is 22

The connection remote needs to rely on the Linux server to install the SFTP service side, generally this SFTP service port is 25

Using an SSH client to connect to a Linux server is a bit like a remote connection under Windows, but Linux has no graphical interface after SSH connection, all commands.

Putty

Xshell

Use the SFTP client to connect to the SFTP server to upload and download files. (Upload the installation package, modify the configuration file upload.) )

WinSCP

Xftp

Two common combinations in the enterprise:

Putty+winscp

Xshell+sftp+manager

Interview: Use Xshell, putty and other SSH client to connect the server, use Xftp, WINSCP and other SFTP client to upload and download files, connect and upload, download must depend on the server SSH, SFTP service, that is, the Linux server needs to start the two services.

2.6.4 have you ever used a cloud host?

Used, in the original company, we didn't use our own server, but rented Ali's cloud host.

Not used, but somewhat understood.

Cloud Host is a number of cloud service operators (Ali, Huawei, Western Digital, Sina, etc.), to provide remote server functions, our developers or enterprises only need to pay on demand to rent the corresponding server.

Use SSH and SFTP for operation

Has 2.6.5 done anything about database optimization?

have done MySQL database optimization, other databases are similar

Find, locate slow queries, and optimize

Optimization means:

1, create the index: Create the appropriate index, we can query in the index, query to find the corresponding record directly.

2, sub-table: When the data of a table is more or a table of some of the field value is more and rarely used, the use of horizontal and vertical tables to optimize.

3, read and write separation: When a server can not meet the requirements, the use of read-write separation of the way to cluster.

4. Caching: Using Redis for caching

5. Some common optimization techniques

2.6.6 Find slow queries and locate slow queries?

Before the project self-test, start the MySQL database with a slow query, and write the slow execution of the statement to the log, after a certain period of time, by viewing the log to find the slow query statement.

Use the explain slow query statement to analyze the problem of the statement in detail

The following paradigm of 2.6.6 database optimization

How the database is designed to be followed

What kind of table is 3NF (normal)

The form of the table is first in line with 1NF to meet the 2NF and further meet the 3NF

1NF: That is, the column of the table is atomic, non-divisible, that is, column information, can not be decomposed, and the database is a relational database (Mysql/oracle/db2/sybase/sql server), automatically meet the 1NF, the relational database is not allowed to split columns.

2NF: The records in the table are unique, usually we design a primary key to implement

3NF: The table does not have redundant data, that is, the information of the table, if it can be deduced, it should not be designed to separate a table field to store.

Anti-3NF: No redundant database is not necessarily the best database, sometimes in order to improve operational efficiency, it is necessary to reduce the paradigm standard, the proper retention of redundant databases, the practice is: in the conceptual database model design to abide by the third paradigm, reduce the standard of the normal work to the physical data model design considerations, Lowering the paradigm is adding fields, allowing redundancy. For example: order and Order items, album views, and number of photos viewed

2.6.7 Choosing the right storage engine

In development, we often use the storage engine myisam/innodb/memory

MyISAM Storage Engine

If the table is not high on things, but also to query and add the main, we consider using the MyISAM storage engine, such as BBS in the posting table, reply to the table.

InnoDB Storage Engine:

High transaction requirements, the data is important data, we recommend the use of InnoDB, such as order form, account form

Memory Storage Engine

Our data changes frequently, do not need storage, but also frequent query and modification, we consider using memory, very fast.

Ask the difference between MyISAM and InnoDB (important)

1. Transaction security MyISAM does not support transactions and InnoDB support

2, query and add speed MyISAM do not need to support transactions to consider the synchronization lock, query and add faster

3, support full-text search MyISAM support, InnoDB not support

4, lock mechanism MyISAM support table lock, INNODB support row lock

5, foreign key MyISAM does not support foreign keys, INNODB support foreign keys (usually not set foreign keys, usually in the program to ensure the consistency of the data)

2.6.8 database optimization To create an appropriate index

Index is a data structure that helps the DBMS efficiently obtain

Category: Normal index/UNIQUE index/PRIMARY key index/full-text index

Normal index: Allow duplicate values to appear

Unique index: Other than normal index (user name, ID, Email,tel) except for duplicate records

Primary KEY index: is created with the primary key set, that is, when a column is set as the primary key, the database will create an index to the column, which is the primary key index, unique and no null value

Full-Text indexing: Used to index text fields (Char,varchar,text) in a table, full-text indexing for MyISAM

Explain select * from Articiles match (title,body) against (' database ') will use full-text indexing

2.6.9 Index Use Tips

Index Disadvantages:

1. Occupy Disk space

2, the DML (insert, modify, delete) operation has influence, slow

Usage scenarios:

A, must be used frequently in the where condition, if do not make the query there is no meaning

b, the field content is not unique to several values (sex)

C, field content does not change frequently

Specific tips:

1, for the creation of multi-column index (composite index), not the first part of the use of the index will not be used

ALTER TABLE Dept Add index My_ind (dname, loc); Dname the left column, Loc is the right column.

Explain select * FROM dept where dname = ' aaa ' is used to index

Explain select * FROM dept where loc = ' AAA ' is not used to index

2. For queries that use like, the query will not use the index if it is '%aaa ', and ' aaa% ' will use the index

Explain select * FROM dept where dname like '%aaa ' does not use the index

Explain select * FROM dept where dname like ' aaa% ' will use the index

Therefore, in the like query, the keyword can not use a character such as% or _, if must be preceded by a change in value, then consider using full-text index

3. If there is or in the condition, the index is not used, even if the conditional index is not used, in other words, all the fields required for use must be used independently.

Explain select * FROM dept where dname = ' aaa ';

Explain select * FROM dept where loc = ' AAA ';

Explain select * FROM dept where deptno = ' 102 ';

SELECT * FROM dept where dname = ' xxx ' or loc = ' xx ';

SELECT * FROM dept where dname = ' xxx ' or deptno = 45;

SELECT * FROM dept where dname = ' xxx ' or loc = ' xx ' or deptno = ' 45 ';

4. If the column type is a string, be sure to reference the data in quotation marks in the condition, otherwise the index will not be used.

Explain select * FROM dept where dname = ' 111 ';

Explain select * FROM dept where dname = 111; Numeric Auto-turn string

Explain select * FROM dept where dname = DDD; Error

5. If MySQL estimates that a full table scan is faster than using an index, the index is not used

There's only one piece of data in the table

2.6.10 Database Optimization sub-table

Sub-table divided into horizontal (by row) table and vertical (by column) table

According to experience, MySQL table data generally reach millions, query efficiency will be very low, easy to cause table lock, and even piled up a lot of connections, directly hanging off; the horizontal table can greatly reduce these pressures. Tables are divided by row data.

If the value of a field in a table is very numerous (long text, binary, and so on), it is queried only in rare cases. At this point, multiple fields can be placed in a single table, associated with a foreign key. Vertical sub-table.

Exam details, generally we only focus on points, not attention to details.

Horizontal sub-table strategy:

1. By Time table

This type of table must have certain limitations, when the data has a strong high efficiency, such as micro-Bo send records, message records, and so on, this data very few users will query the forehead data for several months, such as the monthly table can be divided.

2. Divide the table by the interval range

Generally in a self-increasing ID requirements, such as according to the user_id level of the table

Table_1 user_id from 1~100w

Table_2 user_id from 101~200w

Table_3 user_id from 201~300w

3.hash Sub-table

The table name of the data store table is computed by a certain hash algorithm through the ID or name of the original target, and then the corresponding table is accessed.

10 sheets as follows

Read/write separation of 2.6.11 database optimization

A database supported by the maximum number of concurrent connections is limited, if the user concurrent access too much, a server can not meet the requirements of the cluster processing, MySQL cluster processing technology is most commonly used is read and write separation.

Master-Slave synchronization

The database will eventually persist data to disk, and if the cluster must ensure that the data for each data server is consistent, the operations that can alter the database are written to the primary database, while the other databases synchronize the data from the primary database.

Read/write separation

Use load Balancing to implement write operations to the main database, and read operations to the server.

2.6.12 Database Optimization Cache

A cache layer is added between the Persistence Layer (DAO) number database (db), and if the data accessed by the user is cached, fetched directly from the cache layer when the user accesses it, without accessing the database, and the cache is operating at the memory level, the access speed is fast

Role: Reduce the database server pressure, reduce time.

Common caches in Java are,

1.Hibernate level Two cache, the cache cannot complete the distributed cache

2. You can use Redis as the central cache.

2.6.13 Statement Optimization Tips

DDL Optimizations:

1, by disabling the index to provide import data performance, this operation is mainly for the table with the database, append data

Remove key

ALTER TABLE test3 disable keys;

BULK INSERT Data

INSERT INTO TEST3 select * from test;

Recovery key

ALTER TABLE test3 enable keys;

2, close the only test

Set Unique_checks=0 off;

Set Unique_checks=1 open;

3, modify the transaction submission Method (import) (change multiple times to submit as once)

Set Autocommit=0 off

Set Autocommit=1 Open

DDL optimization

INSERT into test values (from);

INSERT into test values (1,3);

INSERT into test values (1,4);

Combine multiple strips into one

INSERT into test values (1,3), (1,4);

DQL optimization

Order by optimization

1, multi-use index sorting

2. General result sort (non-indexed sort

Group BY optimization

is to use ORDER by NULL to cancel the default sort)

Sub-query optimization

Find customers who are not in the payment list on the customer list

Find customers who are not in the payment list on the customer list, and inquire about customers who have not bought anything

Explain select * from the customer where customer_id not in (select distinct customer_id from payment) #子查询--This kind of thing is based on the Func outer chain

Explain select * FROM customer C left joins payment p on (c.customer_id=p.customer_id) where p.customer_id null--This kind of thing is based on the "index" Outer chain

or optimization

High performance with or on two independent indexes

1, or both sides are judged by the index field, good performance

2, or both sides, there is no side, poor performance

3, if the employee table name and email These two columns is a composite index, but if it is: Name= "A" or email= "@" this way, will not use the index!

Limit optimization

Select Film_id,description from film order by title limit so,s;

Select A.film_id,a.description from film a INNER join (select film_id from film order by title limit so,s) b on a.film_id=b . film.id;

2.6.14 BULK INSERT millions of data how to implement

Principle:

1, change multiple times to submit as one

2. Use batch operation

2.6.15 ever used Redis? What is Redis?

Redis is a key-value nosql database that is stored in memory, persisted to disk based on a certain policy, and does not lose the database with no loss of data type, and supports more

A session is stored as a central cache for data and Web clusters that are used primarily as cache databases.

2.6.16 a brief talk about Redis usage scenarios

Cache:

Place frequently-needed, infrequently-modified data into a fast-reading space (memory) so that the next visit reduces time, relieves stress, and reduces access time.

Counter:

Counters in Redis are atomic memory operations

Can solve the problem of inventory overflow. Invoicing System Inventory Overflow

Session Cache Server:

Web cluster as a session cache server

Cache queue, etc.

How to save 2.6.17redis objects

JSON string

You need to convert the object to a JSON string, and when the string is processed, set or get directly using set, get

Pros: Easy to set up and get

Cons: No specific method required to convert object to JSON (JSONLIB)

Bytes

Serialization is required to serialize the object to a byte save

If the data can be fully json, after all, Redis direct set, get using a lot lower threshold, Redis is not provided with a dedicated set object method, you need to rewrite.

If you're worried about the resource consumption of JSON-to-objects, there are a few things to consider,

1th: There is a performance problem with the JSON conversion Lib used

2nd: Is the data volume level, if the storage of millions big data objects, it is recommended to store the serialized object, if it is a small number of data-level objects, or not many data object fields, or it is recommended to use JSON to convert to string.

After all, Redis is well-optimized for storing character types, depending on the scenario you're using and how you use it.

2.6.18redis Data elimination mechanism

In Redis, Allowing users to set maximum memory size server.maxmemory is useful in memory-constrained scenarios, such as deploying 4 Redis service points on a 8G machine, allocating 1.5G of memory per service point, and reducing memory tension, resulting in more robust services.

Limited memory size required to maintain limited data

When the Redis memory dataset size goes up to a certain size, a data-retirement strategy is implemented, and Redis provides a 6 data-out strategy.

VOLATILE-LRU: Pick the least recently used data from the set of data sets (Server.db[i].expires) that have expired time

Volatile-ttl: Select the data that will expire from the set of expired data sets (Server.db[i].expires)

Volatile-random: Choose data culling from any data set (Server.db[i].expires) that has an expiration time set

ALLKEYS-LRU: Pick the least recently used data culling from the dataset (Server.db[i].dict)

Allkeys-random: Choose data culling from data set (SERVER.DB[I].DICT)

No-enviction (expulsion): Prohibition of eviction data

When Redis determines that a key-value pair is evicted, the data is deleted and the data change message is posted locally (aof persistent) and slave (master-slave connection).

2.6.19Java Accessing Redis

1. Accessing the Redis server using Jedis Java client is a bit like accessing MySQL through JDBC.

2, of course, if Spring is a cluster, you can use spring data to access redis,spring data is just two packages for Jedis

2.6.20redis Cluster

When a data fails to meet the requirements, it can be handled using a Redis cluster, similar to the read/write separation of MySQL

Java Interview ⑦ Advanced section

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.